1073. Scientific Notation (20)

来源:互联网 发布:淘宝店铺每日工作流程 编辑:程序博客网 时间:2024/06/10 01:46

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

将一个用科学记数法表示的数用正常模式表示。要分类讨论,首先先定好符号,符号根据给出字符串的第一个字符来确定。然后取给出字符串的系数和指数。如果指数小于0,则答案的数字的前面部分为"0.",然后根据指数大小补零,最后接上系数就能得到答案的数字部分。如果指数大于等于零,又要分两种情况:如果指数小于系数的小数部分的长度,则答案的数字部分仍带有小数点,根据指数得到小数点的位置从而得到答案的数字部分;如果指数大于或等于系数的小数部分的长度,则要在系数后面补零。符号部分和数字部分相加就得到了答案。


代码:

#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <vector>using namespace std;int main(){string s;cin>>s;string num;int p=-1,n=-1,i;for(i=1;i<s.size();i++){if(s[i]=='E') break;if(s[i]=='.') continue;num+=s[i];}int ex=atoi(s.substr(i+1).c_str());//cout<<ex<<endl;string res=(s[0]=='-')?"-":"";if(ex<0){int m=-ex-1;res+="0.";for(int i=0;i<m;i++) res+="0";res+=num;}else{int m=num.size()-1;if(m>ex) {res=res+num.substr(0,ex+1)+"."+num.substr(ex+1);}else{res+=num;for(int i=0;i<ex-m;i++){res+="0";}}}cout<<res;}


0 0