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

    题解:找到E的位置,分别处理尾数和指数即可
    #include<bits/stdc++.h>
    using namespace std;
    
    int main(){
        string s,s1,s2;
        cin>>s;
        int pos;
        if(s[0]=='-'){
            printf("-");
            s.erase(s.begin());
        }
        else{
            s.erase(s.begin());
        }
        for(int i=0;i<s.length();i++){
            if(s[i]!='E'){
                s1+=s[i];
            }
            else{
                pos=i;
                s2=s.substr(i+1);
                break;
            }
        }
        
        int sum=0;
        for(int i=1;i<s2.length();i++){
            sum=sum*10+(s2[i]-'0');
        }
        if(sum==0){
            cout<<s1<<endl;
            return 0;
        }
        if(s2[0]=='+'){
            cout<<s1[0];
            if(s1.length()-2>sum){
                for(int i=2,j=1;i<s1.length();i++,j++){
                    if(j==sum){
                        printf("%c",s1[i]);
                        printf(".");
                    }
                    else{
                        printf("%c",s1[i]);
                    }
                }
                printf("
    ");
            }
            else{
                cout<<s1.substr(2);
                for(int i=0;i<sum-s1.length()+2;i++){
                    printf("0");
                }
                printf("
    ");
            }
        }
        else{
            printf("0.");
            for(int i=0;i<sum-1;i++){
                printf("0");
            }
            for(int i=0;i<s1.length();i++){
                if(s1[i]=='.'){
                    continue;
                }
                else{
                    printf("%c",s1[i]);
                }
            }
            printf("
    ");
        }
        return 0;
    }
    #include<bits/stdc++.h>
    using namespace std;
    
    int main(){
        string s,s1,s2;
        cin>>s;
        int pos;
        if(s[0]=='-'){
            printf("-");
            s.erase(s.begin());
        }
        else{
            s.erase(s.begin());
        }
        for(int i=0;i<s.length();i++){
            if(s[i]!='E'){
                s1+=s[i];
            }
            else{
                pos=i;
                s2=s.substr(i+1);
                break;
            }
        }
        
        int sum=0;
        for(int i=1;i<s2.length();i++){
            sum=sum*10+(s2[i]-'0');
        }
        if(sum==0){
            cout<<s1<<endl;
            return 0;
        }
        if(s2[0]=='+'){
            cout<<s1[0];
            if(s1.length()-2>sum){
                for(int i=2,j=1;i<s1.length();i++,j++){
                    if(j==sum){
                        printf("%c",s1[i]);
                        printf(".");
                    }
                    else{
                        printf("%c",s1[i]);
                    }
                }
                printf("
    ");
            }
            else{
                cout<<s1.substr(2);
                for(int i=0;i<sum-s1.length()+2;i++){
                    printf("0");
                }
                printf("
    ");
            }
        }
        else{
            printf("0.");
            for(int i=0;i<sum-1;i++){
                printf("0");
            }
            for(int i=0;i<s1.length();i++){
                if(s1[i]=='.'){
                    continue;
                }
                else{
                    printf("%c",s1[i]);
                }
            }
            printf("
    ");
        }
        return 0;
    }

    注:测试点2,3考察点:

    当指数与小数部分相等时,注意“.”,例如:+1.234E+03 ===> 1234(T) 1234.(F)

    易错数据:

    +3.1415E+004   //3.1415

    -3.1415926E+4    //-3.1415.926

    +3.1415926E-01   //0.31415926

    -3.1415926E-0005  //-0.000031415926

     
  • 相关阅读:
    Android平台Qt开发入门教程 狼人:
    PySide教程:第一个PySide应用 狼人:
    MeeGo系统1.2版本新组件 狼人:
    讨论:.NET 4各项技术的应用前景,徐汇区网站设计 狼人:
    为 NokiaQt SDK增加新的Symbian SDK开发平台 狼人:
    Skia引擎API整理介绍(skia in Android 2.3 trunk) 狼人:
    Windows Phone 7 开发之:工具栏 狼人:
    Google Adsense(Google网站联盟)广告申请指南 狼人:
    PySide教程:一个简单的点击按钮示例 狼人:
    内部类类4线程两加两减
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14995302.html
Copyright © 2011-2022 走看看