PAT菜鸡进化史_乙级_1024

PAT菜鸡进化史_乙级_1024

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9].[0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部分的正负号即使对正数也必定明确给出。

现以科学计数法的格式给出实数 A,请编写程序按普通数字表示法输出 A,并保证所有有效位都被保留。

输入格式:

每个输入包含 1 个测试用例,即一个以科学计数法表示的实数 A。该数字的存储长度不超过 9999 字节,且其指数的绝对值不超过 9999。

输出格式:

对每个测试用例,在一行中按普通数字表示法输出 A,并保证所有有效位都被保留,包括末尾的 0。

输入样例1:

+1.23400E-03

输出样例1:

0.00123400

输入样例2:

-1.2E+10

输出样例2:

-12000000000

思路:

感觉我的注释写的还蛮清楚的?
主要怕自己回头望了看不懂了hhh

Code:

#include <iostream>
#include <string>

int main(){
    using namespace std;

// input
    string temp;
    cin >> temp;
    string num, exponent;   // store the number and the exponent
    unsigned int E_index;    // store the index of 'E'
    for (E_index = 0; E_index < temp.size(); E_index++){
        if (temp[E_index] == 'E'){
            num = temp.substr(0, E_index);
            exponent = temp.substr(E_index + 1, temp.size() - 1);
        }
    }
// deal with the input
    int n_exponent = atoi(exponent.c_str());
    int len = num.size();
    if (n_exponent > 0){    // move the point to the right
        if ((len - 3) <= n_exponent)    // if you move too far, you need to add '0', and remove the point
            num.insert(len, n_exponent + 3 - len, '0');
        else    // if you don't need to add '0', just move the point will be okay
            num.insert(n_exponent + 3,1,'.');
        num.erase(2, 1);    // remove the point
        while (num[1] == '0')   // if the number starts with '0'
            num.erase(1, 1);
        if (num[0] == '+')  // remove the beginning '+'
            num.erase(0, 1);
    }else{  // move the point to the left
        num.erase(2, 1);    // remove the point
        int sign = 0;       // deal with the sign, '0' represents '-' and '1' represents '+'
        if (num[0] == '+')
            sign = 1;
        num.erase(0, 1);
        num.insert(0, -n_exponent, '0');    // you must add '0' at the beginning
        num.insert(1, 1, '.');  // add the point
        if (!sign)
            num.insert(0, 1, '-');
    }
// output
    cout << num << endl;

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值