Not Only Algorithm,不仅仅是算法,关注数学、算法、数据结构、程序员笔试面试以及一切涉及计算机编程之美的内容 。。
你的位置:NoAlGo博客 » 题解 » 

PAT 1073. Scientific Notation

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 file 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

解答

#include <cstdio>

int main()
{
	char t[50000]; scanf("%s", t);

	int e = 0, exp;
	while (t[e] != 'E') e++;
	sscanf(t + e + 1, "%d", &exp);

	if(t[0] == '-') printf("-");

	if (exp < 0)
	{
		exp = -exp; printf("0.");
		for (int i = 1; i < exp; i++) printf("0");
		printf("%c", t[1]);
		for (int i = 3; i < e; i++) printf("%c", t[i]);
	}
	else
	{		
		printf("%c", t[1]);
		if (exp < e - 3)
		{
			for (int i = 3; i < 3 + exp; i++) printf("%c", t[i]);
			printf(".");
			for (int i = 3 + exp; i < e; i++) printf("%c", t[i]);
		}
		else
		{
			for (int i = 3; i < e; i++) printf("%c", t[i]);
			for (int i = 0; i < exp - (e - 3); i++) printf("0");
		}
	}

	printf("\n");
	return 0;
}
上一篇: 下一篇:

我的博客

NoAlGo头像编程这件小事牵扯到太多的知识,很容易知其然而不知其所以然,但真正了不起的程序员对自己程序的每一个字节都了如指掌,要立足基础理论,努力提升自我的专业修养。

站内搜索

最新评论