PAT_A 1073 Scientific Notation & PAT_B 1024 科学计数法

PAT_A 1073 Scientific Notation & PAT_B 1024 科学计数法

分析

这两道题除了题目描述的语言不同,其实是讲的内容一样的,因此放在一起了。

本题需要将满足题中正则表达式的科学计数法转换为一般表示法,可按照条件进行转换,注意边界条件

PAT_A 1073 Scientific Notation

题目的描述

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 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

PAT_B 1024 科学计数法

题目的描述

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式

[+-][1-9] . [0-9]+E[+-][0-9]+

即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部分的正负号即使对正数也必定明确给出。

现以科学计数法的格式给出实数 A,请编写程序按普通数字表示法输出 A,并保证所有有效位都被保留。

输入格式:

每个输入包含 1 个测试用例,即一个以科学计数法表示的实数 A。该数字的存储长度不超过 9999 字节,且其指数的绝对值不超过 9999。

输出格式:

对每个测试用例,在一行中按普通数字表示法输出 A,并保证所有有效位都被保留,包括末尾的 0。

输入样例 1:

+1.23400E-03

输出样例 1:

0.00123400

输入样例 2:

-1.2E+10

输出样例 2:

-12000000000

AC的代码

#include<bits/stdc++.h>

using namespace std;

int main(){
    string num,dishu;
    int i=0;
    cin>>num;
    if(num[0]=='-')cout<<'-';
    for(i=1;num[i]!='E';i++){
        if(num[i]!='.')dishu+=num[i];
    }
    int z = stoi(num.substr(++i));
    if(z<0){
        z=-z;
        i+=1;
        cout<<"0.";
        string zeros(z-1,'0');
        cout<<zeros<<dishu;
    }
    else if(z>0){
        i+=1;
        for(i=0;i<dishu.size();i++){
            if(i==z+1)cout<<'.';
            cout<<dishu[i];
        }
        while(i<=z){
            cout<<'0';
            i++;
        }
    }
    cout<<endl;
    return 0;
}
posted @ 2022-01-24 22:53  ghosteq  阅读(22)  评论(0编辑  收藏  举报