zoukankan      html  css  js  c++  java
  • pat1073. Scientific Notation (20)

    1073. Scientific Notation (20)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    HOU, Qiming

    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
    

    提交代码

    科学计数法。

     1 #include<cstdio>
     2 #include<stack>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 using namespace std;
     9 string trans(string s){
    10     int i=1;
    11     while(i<s.length()){//有效数位
    12         if(s[i]=='E'){
    13             break;
    14         }
    15         i++;
    16     }
    17     int index=++i;//指数符号位
    18 
    19     //cout<<index<<endl;
    20 
    21     i++;//指数开始
    22     int exp=0;
    23     //确定指数
    24     while(i<s.length()&&s[i]=='0'){//去掉前0
    25         i++;
    26     }
    27 
    28     //cout<<i<<endl;
    29 
    30     while(i<s.length()){//指数计算
    31         exp*=10;
    32         exp+=s[i]-'0';
    33         i++;
    34     }
    35 
    36     //cout<<exp<<endl;
    37 
    38     string ss;
    39     if(s[index]=='-'){
    40         i=exp-1;
    41         ss="0.";
    42         while(i){
    43             i--;
    44             ss=ss+'0';
    45         }
    46         ss=ss+s[1];
    47         for(i=3;s[i]!='E';i++){
    48             ss=ss+s[i];
    49         }
    50     }
    51     else{//
    52         ss=s[1];
    53         for(i=3;s[i]!='E'&&exp;i++,exp--){
    54             ss=ss+s[i];
    55         }
    56         if(s[i]=='E'){
    57             while(exp){
    58                 ss=ss+'0';
    59                 exp--;
    60             }
    61         }
    62         else{
    63             ss=ss+'.';
    64             while(s[i]!='E'){
    65                 ss=ss+s[i];
    66                 i++;
    67             }
    68         }
    69     }
    70     if(s[0]=='-'){
    71         ss='-'+ss;
    72     }
    73     return ss;
    74 }
    75 int main(){
    76     //freopen("D:\INPUT.txt","r",stdin);
    77     string s;
    78     cin>>s;
    79     //cout<<s<<endl;
    80     cout<<trans(s)<<endl;
    81     return 0;
    82 }
  • 相关阅读:
    poj3294 Life Forms(后缀数组)
    1628. White Streaks(STL)
    1523. K-inversions(K逆序对)
    1890. Money out of Thin Air(线段树 dfs转换区间)
    1350. Canteen(map)
    1521. War Games 2(线段树解约瑟夫)
    1003. Parity(并查集)
    1470. UFOs(三维树状数组)
    1471. Tree(LCA)
    1494. Monobilliards(栈)
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4782315.html
Copyright © 2011-2022 走看看