PTA甲级1073 Scientific Notation (20分)

目录

首先,先贴柳神的博客

https://www.liuchuo.net/ 这是地址

想要刷好PTA,强烈推荐柳神的博客,和算法笔记

原题如下

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

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000

题目大意

一个科学计数法的数字,要你把他转换成为,整数类型

解体思路

① 用两个字符来存储科学计数法的符号 一个数本身的符号,一个是指数的符号

② 科学计数法小于0的时候,就无脑的在前面加0就OK了

③ 科学计数法是正的时候,就要考虑有没有小数的情况了

我的代码(下面有柳神的代码)

#include<iostream>
#include<cstring>
#include <cctype>
#include<string>
using namespace std;
int main(void) {
    char begin,end;
    int i = 0, flag = 0,j=0,number=0;
    string a;
    cin >> a;
    begin = a[i];
    i++;
    while (a[i] != '\0') {
        if (a[i] == 'E') {
            j = i - 2;
            end = a[i + 1];
            break;
        }
        i++;
    }
    number = stoi(a.substr(j + 4));
    //stoi是在#include<string>里面的一个函数,作用是把a的j+4位置到最好的字符串变成数字
    //cout << number<<endl;
    a.erase(a.begin());
    a.erase(a.begin() + 1);
    a.erase(a.begin() + j, a.end());
    if(begin=='-'){
        cout << '-';
    }
    if (end == '+') {
        if (number - j+1>= 0) {
            cout << a;
            for (int i = 0; i <=number - j; i++) {
                cout << '0';
            }
        }
        else {
            string str1 = ".";
            a.insert(number+1, str1);
            cout << a;
        }
    }
    else if(end=='-'){
        cout << '0'<<'.';
        for (int i = 0; i < number - 1; i++)
            cout << '0';
        cout << a;
    }
    return 0;
}

柳神的代码

#include<cstring>
#include <iostream>
#include<string>
using namespace std;
int main() {
    string s;
    cin >> s;
    int i = 0;
    while (s[i] != 'E') i++;    //用E当作分隔符
    string t = s.substr(1, i - 1);  //这个t是前半部分的
    int n = stoi(s.substr(i + 1));  //这个n是后半部分的数字
    if (s[0] == '-') cout << "-";
    if (n < 0) {
        cout << "0.";
        for (int j = 0; j < abs(n) - 1; j++) cout << '0';
        for (int j = 0; j < t.length(); j++)
            if (t[j] != '.') cout << t[j];  //在输出前半部分
    }
    else {  //指数大于0
        cout << t[0];
        int cnt, j;
        for (j = 2, cnt = 0; j < t.length() && cnt < n; j++, cnt++) cout << t[j];
        //输出最多可以去0的指数
        if (j == t.length()) {  //前面的小数的小数部分都没有了
            for (int k = 0; k < n - cnt; k++) cout << '0';  //补零
        }
        else {
            cout << '.';
            for (int k = j; k < t.length(); k++) cout << t[k];
        }
    }
    return 0;
}

1073 Scientific Notation (20分)

上一篇:pycharm科学模式简介和使用


下一篇:PAT甲级——1073 Scientific Notation (20分)