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;
    } 
    
    
  • 相关阅读:
    关于devDependencies和dependencies报错提示及区别 --save 和--save-dev 的区别
    git 创建分支并提交到远程
    静态类中,静态方法和静态变量的执行顺序按出现执行
    Java和C#语法对比
    大数据时遇到的问题
    Javascript技巧笔记
    Javascript特性笔记
    Javascript 之《函数传参到底是值传递还是引用传递》
    Javascript之《创建对象》
    IE之诡异行为
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8399413.html
Copyright © 2011-2022 走看看