zoukankan      html  css  js  c++  java
  • PAT 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 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

    #include<iostream>
    #include<algorithm> 
    #include<string.h>
    using namespace std;
    bool Iszero(char c){
        return c=='0';
    }
    int main(){
        string num,zhishu; // num用来储存输入,zhishu为了后面保存string形式的E的指数
        zhishu.resize(4); // 没有这一步,copy(it+1,num.end(),zhishu.begin())是错的,因为copy的输入序列至少能保存下copy的内容啊
        cin>>num; 
        int point_num=0; //point_num记录小数位有多少位
        char c=num[0]; // 用来保存正负号 
        auto it=find(num.begin(),num.end(),'E'); // 利用包含在头文件<algorithm>中的find函数返回指向E的迭代器
        copy(it+1,num.end(),zhishu.begin()); // copy'E'之后的指数部分进入zhishu
        num.erase(it,num.end()); //把num中E到最后的部分删掉
        point_num=num.length()-3; //小数位位数
        int zhishu2=stoi(zhishu); //把string形式的指数转化为int
        point_num-=zhishu2; //拿原来的小数位数减去指数,如果为负的,说明没有小数点了并且要在后面加零。为正的说明结果的小数位数
        num.erase(find(num.begin(),num.end(),'.')); // 删除原来的小数点
        if(point_num>0){
            if(point_num>=num.length()-1)
            num.insert(num.begin()+1,point_num-(num.length()-1)+1,'0');
            num.insert(num.end()-point_num,1,'.');
        }
        else{
            num.insert(num.end(),-point_num,'0');
        }
    //下面是处理结果前面可能存在无意义的零,比如001.003, 000.002 等等
    ********************************************************
        auto point=find(num.begin(),num.end(),'.');
        if(point!=num.end()){
            it=find_if_not(num.begin()+1,point,Iszero);
            if(it==point) num.erase(num.begin(),it-1);
            else num.erase(num.begin(),it);
        }
        else{
            it=find_if_not(num.begin()+1,num.end(),Iszero);
            num.erase(num.begin(),it);
        }
    ***********************************************************
        if(c=='-') // 望读者不要和我一样ԅ(¯﹃¯ԅ)
        num.insert(num.begin(),1,c);
        cout<<num<<endl;
        return 0;
    } 
    
    
  • 相关阅读:
    PAAS平台的web应用性能测试与分析
    thrift之TTransport层的分帧传输类TFramedTransport
    thrift之TTransport层的缓存传输类TBufferedTransport和缓冲基类TBufferBase
    thrift之TTransport层的堵塞的套接字I/O传输类TSocket
    ssh证书登录(实例详解)
    [转]个性化推荐技术的十大挑战
    Github上最全的APICloud开源前端框架效果盘点(转)
    api.connectionType 判断当前网络技术经验
    安卓苹果获取时间戳转成本地时间
    js获取网络图片的宽和高
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8399413.html
Copyright © 2011-2022 走看看