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-03Sample Output 1:
0.00123400Sample Input 2:
-1.2E+10Sample Output 2:
-12000000000
提交代码
细分类,认真模拟。段错误就把数组开大点。
#include<stdio.h>
#include<string.h>
int main()
{
int i;
char str[1000000],r[1000000],exsign,exponent[1000000],ans[1000000];
memset(str,0,sizeof(str));memset(r,0,sizeof(r));
memset(exponent,0,sizeof(exponent));memset(ans,0,sizeof(ans));
scanf("%s",str);
int lenstr=strlen(str);
if(str[0]=='-')
printf("-");
int cntr=0;
for(i=1;str[i]!='E'&&i<lenstr;i++) //保存底数在r[]中
r[cntr++]=str[i];
i++; //指向指数的符号
exsign=str[i];
i++; //指向指数的第一个数据位
int cntex=0;
for(;i<lenstr;i++) //保存指数在exponent[]中
exponent[cntex++]=str[i];
int sum=0;
for(i=0;i<cntex;i++)
sum=sum*10+exponent[i]-'0';
// puts(r);
// puts(exponent);
// printf("%c&&&%d",exsign,sum);
int cntans=0;
if(exsign=='-'){
for(i=cntr-1;i>=0;i--){
if(r[i]!='.')
ans[cntans++]=r[i];
}
for(i=0;i<sum-1;i++) //左边再补上sum-1个0
ans[cntans++]='0';
ans[cntans++]='.';
ans[cntans++]='0';
for(i=cntans-1;i>=0;i--)
printf("%c",ans[i]);
printf("\n");
}
if(exsign=='+'){
if(sum<cntr-2){
ans[cntans++]=r[0];
for(i=2;i<sum+2;i++){ //第三位下标实际上是2!
ans[cntans++]=r[i];
}
ans[cntans++]='.';
for(i=sum+2;i<cntr;i++)
ans[cntans++]=r[i];
for(i=0;i<cntans;i++)
printf("%c",ans[i]);
printf("\n");
}
if(sum>=cntr-2){
for(i=0;i<cntr;i++){
if(r[i]!='.')
ans[cntans++]=r[i];
}
for(i=0;i<sum-(cntr-2);i++)
ans[cntans++]='0';
for(i=0;i<cntans;i++)
printf("%c",ans[i]);
printf("\n");
}
}
return 0;
}