【笨方法学PAT】1073 Scientific Notation (20 分)

版权声明:欢迎转载,请注明来源 https://blog.csdn.net/linghugoolge/article/details/83313267

一、题目

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9].[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent's signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input Specification:

Each input contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent's absolute value is no more than 9999.

Output Specification:

For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros.

Sample Input 1:

+1.23400E-03

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000

二、题目大意

科学计数法转为普通数字。

三、考点

string,模拟

四、注意

1、使用int会导致越界,这里的代码有问题,最后一点没有通过,需要改成string进行处理。//TODO

五、代码

#pragma warning(disable:4996)
#include<iostream>
#include<string>
using namespace std;
int main() {
	//read
	char c1, c2;
	long long int a, b, c;
	scanf("%c%lld.%lldE%c%lld", &c1, &a, &b, &c2, &c);

	if (c1 == '-')
		cout << '-';

	//-
	if (c2 == '-') {
		cout << "0.";
		for (int i = 0; i < c - 1; ++i)
			cout << 0;
		cout << a << b;
	}

	//+
	else {
		cout << a;
		string s = to_string(b);

		//no dot
		if (s.length() <= c) {
			cout << b;
			for (int i = 0; i < c - s.length(); ++i)
				cout << 0;
		}
		//have dot
		else {
			int i;
			for (i = 0; i < c; ++i)
				cout << s[i];
			cout << ".";
			for (; i < s.length(); ++i)
				cout << s[i];
		}
	}

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/linghugoolge/article/details/83313267