PAT甲级 1073 Scientific Notation

#include<bits/stdc++.h>

using namespace std;

struct Num {
    string str;
    int point;
    Num(string & n) : str(n){
        if(str[0] == '-')
            cout << "-";
        str = str.substr(1); // 自此忽略掉符号

        int exp_idx = str.find('E');
        auto index = stoi(str.substr(exp_idx + 1));
        point = str.find('.'); // 题目的正则表达式保证一定含小数点
        str.erase(exp_idx + str.begin(), str.end());
        if(index == 0){ // 指数为0,则直接输出
            cout << str << endl;
            return;
        }
        str.erase(point, 1); // 去除掉小数点
        if(index < 0) // 指数小于0,则小数点向左移动
            point += index - 1;
        else {
            point = index - (exp_idx - 1 - point);
            if(point < 0) {   // 小数点向右移动,但不能使所有数位不能全部变成整数
                str.insert(point + exp_idx - 1 + str.begin(), '.');
                cout << str << endl;
                return;
            }
        }
//         cout << point << endl;
        if(point <= 0) {
            int n_p = abs(point);
            for(int i = 0; i < n_p; ++i) {
                cout << "0";
                if(i == 0)
                    cout << ".";
            }
            cout << str << endl;
        } else {
            cout << str;
            cout << string(point, '0') << endl;
        }
    };
};

int main() {
    string str;
    cin >> str;
    Num num(str);
}

提交

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值