PAT Level B 1024 Scientific Notation (Problem Solving Ideas + AC Code)

topic:

Scientific notation is a convenient way for scientists to represent very large or small numbers, which satisfy the regular expression [±][1-9][ .0-9]+E[±][0-9] +, that is, the integer part of the number has only 1 digit, and the fractional part has at least 1 digit. The sign of the number and its exponent part must be clearly given even for positive numbers.

Given the real number A in the format of scientific notation, please write a program to output A in the normal number notation , and ensure that all significant digits are reserved.

Input format:

Each input contains 1 test case, a real number A expressed in scientific notation . The storage length of this number does not exceed 9999 bytes, and the absolute value of its exponent does not exceed 9999.

Output format:

For each test case, output A in normal number notation on one line , ensuring that all significant digits are preserved, including the trailing 0.

Input sample 1:

+1.23400E-03

Output sample 1:

0.00123400

Input sample 2:

-1.2E+10

Output sample 2:

-12000000000

Code length limit 16 KB

Time limit 200 ms

Memory limit 64 MB

 

problem solving ideas

Main problem-solving ideas:

  • When the symbol behind E is -, add a 0 at the beginning, then add one ., and add a total of several 0s according to the index of E.
  • When the symbol behind E is +.
    • If the exponent of E is less than the length after the decimal point of the coefficient, add it at the corresponding position ..
    • If the exponent of E is equal to the length of the coefficient after the decimal point, remove and .only output the coefficient.
    • If the exponent of E is greater than the length after the decimal point of the coefficient, add a few 0s if there are more.

Related instructions:

  1. The case where the index of E is 0 is not considered because it is meaningless.
  2. When inputting, an identifier is used [^E], and its meaning is the same as %s, but instead of encountering a space or a carriage return, it ends with an E character.

 

AC code

#include <bits/stdc++.h>
using namespace std;

int main()
{
    
    
    char flag1, flag2, c1;
    char cc[10000];
    int c2;
    scanf("%c%c.%[^E]E%c%d", &flag1, &c1, cc, &flag2, &c2);
    string s;
    if (flag2 == '-')
    {
    
    
        for (int i = 0; i < c2; i++)
        {
    
    
            s += '0';
            if (0 == i)
            {
    
    
                s += '.';
            }
        }
        s += c1;
        s += cc;
    }
    else
    {
    
    
        s += c1;
        s += cc;
        int len  = s.length();
        if (c2 < len -1)
        {
    
    
            s.insert(c2 + 1, ".");
        }
        else 
        {
    
    
            for (int i = 0; i < c2 - len + 1; i++)
            {
    
    
                s += '0';
            }
        }
    }
    if (flag1 == '-')
    {
    
    
        s.insert(0, "-");
    }
    cout << s;
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_70103775/article/details/130119289