PAT 1073 Scientific Notation[字符串处理][科学记数法]

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

题目大意:将科学记数法转换为正常表示的数。由于给的范围都比较大,肯定只能用字符串来表示了

//之前见过一道科学记数法的题目呢,最关键的就是小数点的位置和首位非0元的位置。

pat上一次就AC,牛客网上也通过了,开心。

#include <iostream>
#include<stdlib.h>
#include <string>
#include<cmath>
using namespace std;

int main()
{
    string s;
    cin>>s;
    //找到指数。
    int pos=s.find("E");
    int expo=atoi(s.substr(pos+1).c_str());
    //如果指数是负数,那么就往前加0;
    if(expo<0){
        int p=s.find(".");
        s.erase(p,1);
        s.insert(1,"0.");
        int to=abs(expo)+2;
        for(int i=3;i<to;i++){
            s.insert(i,"0");
        }
        s=s.substr(0,pos+abs(expo));
        //cout<<s;

    }else if(expo>0){
        //找到小数点后有几位
        int len=pos-3;
       // cout<<len<<‘\n‘;
        if(len>expo){
            s.insert(expo+3,".");
            s.erase(2,1);
            s=s.substr(0,pos);
        }else{//len<=expo
            int p=expo-len;
            s=s.substr(0,pos);
            for(int i=0;i<p;i++){
                s+="0";//怎么把0放进去呢?
            }
            s.erase(2,1);
        }

    }
    if(s[0]==‘+‘)
        s=s.substr(1);
    cout<<s;
    return 0;
}
//  +1.2345E03
//  -1.2E+10

主要就是几种情况的考虑:

1.当指数<0的时候,往前补0就可以了

2.当指数>0的时候,分两种情况:一种是小数点后的位数=<指数的,那么需要去小数点后,在后面补0;另一种是小数点后的位数>指数的,那么需要挪动小数点就可以了。

3.对stoi和atoi有了更深的认识,我用的编译器,不支持stoi(头文件为#include<string>),而支持aoti(头文件为#include<stdlib.h>)

4.atoi(s.substr(pos+1).c_str());因为后者是C中的函数,那么需要对字符串进行转换成C中形式,就是调用c_str()函数。

另外还有一点,我的代码中并没有判断指数为0的情况。。明显样例中并没有给出这样的情况:

。。。

查看了柳神的代码:https://www.liuchuo.net/archives/2061

进行了判断指数是否为0。

原文地址:https://www.cnblogs.com/BlueBlueSea/p/9580429.html

时间: 2024-10-10 10:09:34

PAT 1073 Scientific Notation[字符串处理][科学记数法]的相关文章

PAT 1073. Scientific Notation (20)

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

[PAT] 1073 Scientific Notation (20 分)Java

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 lea

PAT(A) 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

PAT甲级——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 lea

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

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 lea

PAT甲题题解-1073. Scientific Notation (20)-字符串处理

题意:给出科学计数法的格式的数字A,要求输出普通数字表示法,所有有效位都被保留,包括末尾的0. 分两种情况,一种E+,一种E-.具体情况具体分析╮(╯_╰)╭ #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <cmath> #define POSITIVE 1 #define NEGATIVE 2 using names

pat(A) 1073. Scientific Notation(水题)

1.链接:点击打开链接 2.代码: #include<cstdio> #include<cstring> #include<iostream> using namespace std; char s[100000]; char ss[100000]; int f[10]; int main() { f[0]=1; for(int i=1; i<9; i++) { f[i]=f[i-1]*10; } while(scanf("%s",s)==1)

PAT (Advanced Level) 1073. Scientific Notation (20)

简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; char s[100000]; int p;