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;
    } 
    
    
  • 相关阅读:
    SQLServer 高可用、高性能和高保护延伸
    SQLServer 通过DMV实现低影响的自动监控和历史场景追溯
    查找表包含的页和页所在的表
    出身在二三线城市软件工作者的悲哀
    SQL语句实现取消自增列属性
    基于Apache(without ssl)的svn环境搭建
    sqlite3 命令行操作
    HTML常用特殊符号集
    IOS项目目录结构和开发流程
    Mac OSX 快捷键&命令行
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8399413.html
Copyright © 2011-2022 走看看