栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【PAT B-1024】科学计数法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【PAT B-1024】科学计数法

【PAT B-1024】科学计数法 C++代码
#include 
using namespace std;
using gg = long long;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    regex r("([+-])([1-9]).([0-9]+)E([+-])([0-9]+)");
    string si;
    cin >> si;
    smatch result;
    regex_match(si, result, r);
    if (result.str(1) == "-") {  //输入是负数要输出负号
        cout << result[1];
    }
    gg n = stoll(result.str(5));  //获取指数的值
    if (result.str(4) == "-") {  //小数点要左移n位,要在小数点后补n-1个0
        cout << "0." << string(n - 1, '0') << result[2] << result[3];
    } else if (n < result.length(3)) {
        //小数点右移的位数不超过3号子串长度,将3号子串用小数点分隔开进行输出
        cout << result[2] << result.str(3).substr(0, n) << "."
             << result.str(3).substr(n);
    } else if (n == result.length(3)) {
        //小数点右移的位数等于3号子字符串长度,小数点无需输出
        cout << result[2] << result[3];
    } else {
        //小数点右移的位数超过3号子串长度,在3号子串后补0
        cout << result[2] << result[3] << string(n - result.length(3), '0');
    }
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/889421.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号