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 <cstdio>
#include <cstring>
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雲方》與方塊來次消除大戰!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章