码迷,mamicode.com
首页 > 其他好文 > 详细

pat1073. Scientific Notation (20)

时间:2015-09-04 21:12:41      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

pat1073. Scientific Notation (20)

标签:

原文地址:http://www.cnblogs.com/Deribs4/p/4782315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!