PAT 1073 Scientific Notation

来源:互联网 发布:天狼星期货软件管网 编辑:程序博客网 时间:2024/06/11 20:06

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<stdio.h>

#include<string.h>
int main()
{
    int i,j,cnt=0,sum=0,len;
    char num[100000],temp[100000],sign_n,sign_e,neg='-';
    scanf("%s",num);len=strlen(num);
    sign_n=num[0];
    for(i=1,j=0;i<len;i++)
    {
        
        if(num[i]=='E')
            break;
        if(i!=2)
            temp[j++]=num[i];
        if(i>2)cnt++;
    }
    if(sign_n==neg)
        putchar(neg);
    sign_e=num[++i];
    while(num[++i]==48&&i<len);
    for(;i<len;i++)
        sum=sum*10+(num[i]-48);
    if(sign_e==neg)
    {
        putchar(48);
        putchar('.');
        while(--sum)putchar(48);
        printf("%s",temp);
    }
    else
    {
        if(cnt>sum)
        {
            for(i=0;i<=sum;i++)
                putchar(temp[i]);
            putchar('.');
            for(;i<=cnt;i++)
                putchar(temp[i]);
        }
        else
        {
            //printf("sum=%d cnt=%d\n",sum,cnt);
            printf("%s",temp);
            for(i=1;i<=sum-cnt;i++)
                putchar(48);
        }
    }
    putchar('\n');
    return 0;
}
0 0
原创粉丝点击