1073. Scientific Notation (20)【字符串操作】——PAT (Advanced Level) Practise

题目信息

1073. Scientific Notation (20)

时间限制100 ms
内存限制65536 kB
代码长度限制16000 B

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

解题思路

字符串处理,找到E的位置然后进行相应的补全或其他操作即可

AC代码

#include 
#include 
int main()
{
    char s[11000];
    gets(s);
    if (s[0] == '-'){
        putchar(s[0]);
    }
    int p = 1, ep = 0, e;
    while (s[++p]){
        if (s[p] == 'E'){
            ep = p;
        }
    }
    sscanf(s + ep + 1, "%d", &e);
    if (e >= 0) {
        putchar(s[1]);
        int i;
        for (i = 3; i < ep && e-- > 0; ++i){
            putchar(s[i]);
        }
        while (e-- > 0) putchar('0');
        if (i < ep) putchar('.');
        while (i < ep) putchar(s[i++]);
        putchar('\n');
    }else{
        printf("0.");
        while (++e <= -1) putchar('0');
        putchar(s[1]);
        int i = 3;
        while (i < ep) putchar(s[i++]);
        putchar('\n');
    }
    return 0;
}

个人游戏推广:
《10云方》与方块来次消除大战!

你可能感兴趣的:(浙大PAT,浙大Pat)