zoukankan      html  css  js  c++  java
  • PAT(A) 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 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 <cstdio>
    #include <cstring>
    /*
    [+-][1~9].[0~9]E[+-][0~9]+
    in  +1.23400E-03    (03:exp)
    out 0.00123400
    in  -1.2E+10        (10:exp)
    out -12000000000
    
    注.定位E的位置pos, 再按指数正负分两种情况讨论
    1) 指数为负:这种情况一定是输出 0.00...0XXX, 其中小数点后连续的0的个数为exp-1, XXX即为E前面的所有数字
    2) 指数为正:考虑小数点移动后的位置
               当exp!=0时,小数点应该添加在原标号为exp+2的数字后(下标从0开始)
               - 注:若原小数点和E之间的数字个数(pos-3)等于小数点右移位数exp,说明小数正好在整个数的最右边,不需要输出小数点
               最后考虑由于exp较大时需要额外输出exp-(pos-3)个0的步骤
    */
    int main()
    {
        char str[10010];
        gets(str);
        int len=strlen(str);
        if(str[0]=='-') printf("-");    //是负数,输出负号
    
        int pos=0;      //存放字符串中E的位置
        while(str[pos]!='E')  pos++;
    
        int exp=0;      //存放指数(先不考虑正负)
        for(int i=pos+2; i<len; i++)    //exp起始于pos(E的位置)后面两位
            exp=exp*10+(str[i]-'0');    //划重点(1):字符串转化成数字
        if(exp==0){     //特判指数为0的情况for(int i=1; i<pos; i++)
                printf("%c", str[i]);
        }
    
        //开始按指数正负分两种情况讨论
        if(str[pos+1]=='-'){    //如果指数为负
            printf("0.");
            for(int i=0; i<exp-1; i++)  //输出exp-1个0 (eg. 1.23*E^-3==0.00123)
                printf("0");
            printf("%c", str[1]);       //输出除了小数点以外的数字(原整数部分&小数部分)
            for(int i=3; i<pos; i++)
                printf("%c", str[i]);
        }
        else{                  //如果指数为正
        
    for(int i=1; i<pos; i++){ //输出小数点移动之后的数
          
    if(str[i]=='.') continue; //划重点(2):略过原小数点 printf("%c", str[i]); //输出当前数位 //(1)后面不需补0的情况:小数点加在位置exp+2上(2为str[]中小数点的初始位置)
           //  且原小数点和E之间的数字个数(pos-3)不能等于小数点右移位数exp
    if(i==exp+2 && pos-3!=exp) printf("."); } //(2)后面需补0的情况:如果指数exp较大,输出多余的0,末尾不用写小数点 for(int i=0; i<exp-(pos-3); i++) printf("0"); } return 0; }
  • 相关阅读:
    C语言-10-位域与共用体
    python-并发编程
    计算机操作系统
    网络编程-Socket
    网络编程-基础
    python-面向对象进阶
    python-面向对象
    python-模块分类与导入
    python-函数进阶
    python-函数内置方法
  • 原文地址:https://www.cnblogs.com/claremore/p/6548457.html
Copyright © 2011-2022 走看看