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

PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)

时间:2019-11-13 23:46:53      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:不能   传统   content   代码   output   lan   match   col   for   

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

题意:

题目要求对给定的科学计数法进行解析,并且输出传统计数法表示的数字,要求正数不带正号,小数保留原来的后缀0个数。

题解:

先找到E的位置,然后计算出指数,再根据指数大小和E的位置输出结果,该补0补0,改有小数点的位置不能忘了小数点,因为就算指数是正数,也有可能结果还是小数。虽然起初考虑了,但是小数点点的位置算错了,测试点4就没过,之后发现了,是计算错误。

AC代码:

 

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
string a;
int main(){
    cin>>a;
    int l=a.length();
    int e=-1;
    int zhi=0;
    for(int i=0;i<l;i++){
        if(a[i]==E){//找到E的位置
            e=i;
            i+=2;
        }
        if(e!=-1){//算出指数的大小
            zhi=zhi*10+a[i]-0;
        }
    }
    int zuo=0;//小数点往左边调还是右边调
    if(a[e+1]==-) zuo=1;    
    if(a[0]==-) cout<<-;//是负数先输出负号
    if(zuo==1){//左调的情况
        cout<<"0.";
        for(int i=1;i<=zhi-1;i++){//在0.后面输出(指数-1)个0
            cout<<"0";
        }
        for(int i=1;i<e;i++){//忽略.输出底数
            if(a[i]==.) continue;
            else cout<<a[i];
        }
    }else{
        if(zhi>=e-3)
        {
            for(int i=1;i<e;i++){//忽略.输出底数
                if(a[i]==.) continue;
                else cout<<a[i];
            }
            for(int i=1;i<=zhi-(e-3);i++) cout<<0;//补0
        }else{
            for(int i=1;i<e;i++){//测试点4仍会是小数,要在指数+3的地方输出.
                if(i==zhi+3) cout<<".";
                if(a[i]==.) continue;
                else cout<<a[i];
            }
        }
    }
    return 0;
}
    

 

 

 

PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)

标签:不能   传统   content   代码   output   lan   match   col   for   

原文地址:https://www.cnblogs.com/caiyishuai/p/11854118.html

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