1073. Scientific Notation (20)

题目:

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

注意:
1、由于这里输入的有效数据位很长,所以不能直接用数字类型,而应该用字符串来处理。
2、代码中k是10的幂级次,我是分成k=0,k<0,k>0三种情况分别处理。
3、科学技术法中小数点肯定是在第一位有效数字的后面。
4、注意小数点的输出。
5、考虑+1.23400E+3时应该如何输出。

代码:
//1073
#include<iostream>
using namespace std;

int main()
{
	char num[10000];
	int k;//
	char positive;//'+' or '-' flag
	scanf("%c",&positive);
	int i=0;//record the effective length of num
	while(1)
	{
		scanf("%c",num+i);
		if(num[i]=='E')
			break;
		if(num[i]!='.')
			++i;
	}
	num[i]='\0';
	scanf("%d",&k);
	if(positive=='-')
		printf("%c",positive);
	if(k==0)
	{//k=0, print the original num with dot
		printf("%c.",num[0]);
		int j=1;
		while(num[j]!='\0')
			printf("%c",num[j++]);
	}
	else if(k<0)
	{//if k<0, print corresponding number of '0' before num
		printf("0.");
		for(int j=0;j<-k-1;++j)
			printf("0");
		printf("%s",num);
	}
	else
	{//if k>0, be care of dot and '0' int the back
		for(int j=0;j<i || j<=k;++j)
		{//i is the effective length of num
			if(j==k+1)
				printf(".");
			if(j>=i)//the length of effective digit is less than k
				printf("0");
			else
				printf("%c",num[j]);
		}
	}
	printf("\n");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值