PAT甲级刷题记录——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

思路

这题也很简单哈,主要就是分情况讨论:

  • 因为保证科学计数法的整数部分只有一位数,因此,如果此时它的指数是负数,那么直接补指数-1个“0”(小数点到整数部分的首个数字之间的“0”),然后再插入小数点,再插入0即可(即:0.00xxx);
  • 如果它的指数是正数,那么就要分情况讨论啦:
    1.如果科学计数法的小数部分的长度比指数的大小还大,那么说明计算之后的结果小数点还在,这个时候只要计算把小数点插在哪儿就行了~
    2.如果科学计数法的小数部分的长度比指数的大小要小,那么就说明要进行补0的操作,0的个数应该是指数-小数部分长度(我这里的代码写得有问题哈,我只写了在后面补指数-1个“0”,这应该是不对的);
    3.如果科学计数法的小数部分的长度等于指数的大小,那么直接删了“E”后面的内容(包括“E”)以及小数点就行了。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    string ori;
    cin>>ori;
    bool Positive = false;
    if(ori[0]=='+') Positive = true;
    ori.erase(ori.begin());//去字符
    int PosPoint = ori.find(".");
    int PosE = ori.find("E");
    string xiaoshubufen = ori.substr(PosPoint+1, PosE-2);
    ori.erase(ori.begin()+1);//去小数点
    PosE = ori.find("E");//更新一下字符E的位置
    string a = ori.substr(0, PosE);
    string b = ori.substr(PosE+1);
    int zhishu = stoi(b);
    if(zhishu<0){
        for(int i=1;i<=abs(zhishu)-1;i++){
            a.insert(0, "0");
        }
        a.insert(0, ".");
        a.insert(0, "0");
        if(Positive==false) a.insert(0, "-");
    }
    else{
        if(Positive==false) a.insert(0, "-");
        if(xiaoshubufen.length()<zhishu){
            for(int i=1;i<=zhishu-1;i++){
                a += "0";
            }
        }
        else if(xiaoshubufen.length()>zhishu){
            a.insert(zhishu+2 ,".");
        }
    }
    cout<<a;
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值