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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值