【PAT甲级 - C++题解】1073 Scientific Notation

简介: 【PAT甲级 - C++题解】1073 Scientific Notation

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][+−][1−9].[ 0 − 9 ] + E [ + − ] [ 0 − 9 ] + [0-9]+E[+-][0-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
• 1

Sample Output 1:

0.00123400


Sample Input 2:

-1.2E+10
• 1

Sample Output 2:

-12000000000


思路


先处理字符串的正负号,如果是负号直接输出,如果是正号则忽略。

找到 E 在字符串中的位置 k ,取出 . 左右的数字并合在一起得到字符串 a ,例如 1.234 1.2341.234 => 1234 12341234 。

将 E 之后的指数取出来,并转换成整数 num,这里要用到的是 c++ 自带的函数 stoi ,它的功能是将字符串转换成整数。并且为了方便后续处理,我们这里将 num 加上 1 。

判断指数的大小:

如果指数为负数,则小数点需要往左移动 -num 位(注意这里的 num 之前已经加一了)。例如,+ 1.234 E − 03 +1.234E-03+1.234E−03 => 0.001234 0.0012340.001234 。

如果指数大小大于等于 a 的长度,则需要在 a 的后面加上 num - a.size() 个 0(注意这里的 num 之前已经加一了),例如,+ 1.234 E + 04 +1.234E+04+1.234E+04 => 12340 1234012340 。

如果指数大小大于 0 但是小于等于 a 的长度,则需要将 a 用 . 划分成左右两部分,左部分是 0 ~ num ,右部分是 num 以后的数字。例如,+ 1.234 E + 02 +1.234E+02+1.234E+02 => 12.34 12.3412.34 。

输出 a 即为最终结果。


代码

#include<bits/stdc++.h>
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;
}
目录
相关文章
|
5月前
|
C++
C++中的setprecision: fixed: scientific等函数
C++中的setprecision: fixed: scientific等函数
57 0
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
66 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
82 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
77 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
76 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
79 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
80 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
136 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
58 0
|
存储 人工智能 C++
【PAT甲级 - C++题解】1085 Perfect Sequence
【PAT甲级 - C++题解】1085 Perfect Sequence
72 0