• 【PAT甲级】1073 Scientific Notation


    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
    📚专栏地址:PAT题解集合
    📝原题地址:题目详情 - 1073 Scientific Notation (pintia.cn)
    🔑中文翻译:科学计数法
    📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
    ❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

    1073 Scientific Notation

    Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [ + − ] [ 1 − 9 ] [+-][1-9] [+][19]. [ 0 − 9 ] + E [ + − ] [ 0 − 9 ] + [0-9]+E[+-][0-9]+ [09]+E[+][09]+ 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
    
    • 1

    Sample Output 1:

    0.00123400
    
    • 1

    Sample Input 2:

    -1.2E+10
    
    • 1

    Sample Output 2:

    -12000000000
    
    • 1

    思路

    1. 先处理字符串的正负号,如果是负号直接输出,如果是正号则忽略。
    2. 找到 E 在字符串中的位置 k ,取出 . 左右的数字并合在一起得到字符串 a ,例如 1.234 1.234 1.234 => 1234 1234 1234
    3. E 之后的指数取出来,并转换成整数 num,这里要用到的是 c++ 自带的函数 stoi ,它的功能是将字符串转换成整数。并且为了方便后续处理,我们这里将 num 加上 1
    4. 判断指数的大小:
      1. 如果指数为负数,则小数点需要往左移动 -num 位(注意这里的 num 之前已经加一了)。例如, + 1.234 E − 03 +1.234E-03 +1.234E03 => 0.001234 0.001234 0.001234
      2. 如果指数大小大于等于 a 的长度,则需要在 a 的后面加上 num - a.size()0(注意这里的 num 之前已经加一了),例如, + 1.234 E + 04 +1.234E+04 +1.234E+04 => 12340 12340 12340
      3. 如果指数大小大于 0 但是小于等于 a 的长度,则需要将 a. 划分成左右两部分,左部分是 0 ~ num ,右部分是 num 以后的数字。例如, + 1.234 E + 02 +1.234E+02 +1.234E+02 => 12.34 12.34 12.34
    5. 输出 a 即为最终结果。

    代码

    #include
    using namespace std;
    
    int main()
    {
        string x;
        cin >> x;
    
        if (x[0] == '-')   cout << '-';	//处理正负号
    
        int k = x.find('E');	//找到E的位置
        string a = x[1] + x.substr(3, k - 3);	//取出有效数字
        string b = x.substr(k + 1);	//取出指数
        int num = stoi(b);	//stirng -> int
        num++;	//加1,方便后续处理
    
        if (num <= 0)	//指数为负
            a = "0." + string(-num, '0') + a;
        else if (num >= a.size())	//指数大于等于a的长度
            a += string(num - a.size(), '0');
        else	//指数大于0且小于a的长度
            a = a.substr(0, num) + '.' + a.substr(num);
    
        cout << a << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    3D 生成重建007-Fantasia3D和Magic3d两阶段玩转文生3D
    零时科技 || Victor the Fortune攻击事件分析
    Vue3中的computed函数详解
    Java中的数据类型与变量
    理解Go语言延迟执行语句defer关键字
    再见 Xshell 这款开源的终端工具逼格更高
    Vue3 ref函数和reactive函数
    mysql之搭建MHA架构实现高可用
    go语言学习笔记
    波奇学C++:继承
  • 原文地址:https://blog.csdn.net/Newin2020/article/details/126494722