PAT B1024/A1073 Scientific Notation

PAT B1024/A1073 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].[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

参考代码:

 1 /****************************************************
 2 PAT B1024/A1073 Scientific Notation
 3 ****************************************************/
 4 #include <iostream>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 int main() {
10     string scientificNum;
11 
12     cin >> scientificNum;
13 
14     //获取符号‘E'的位置
15     int charEPos = scientificNum.find('E');
16 
17     //获得指数绝对值
18     int exp = 0;
19     for (int i = charEPos + 2; i < scientificNum.size(); ++i) {
20         exp = exp * 10 + scientificNum[i] - '0';
21     }
22     
23     //指数位0 直接输出
24     if (exp == 0) {
25         for (int i = 0; i < charEPos; ++i) {
26             cout << scientificNum[i];
27         }
28     }
29 
30     //输出数值部分的符号
31     if (scientificNum[0] == '-') {
32         cout << '-';
33     }
34 
35     //指数为正数
36     if (scientificNum[charEPos + 1] == '+') {
37         for (int i = 1; i < charEPos; ++i) {
38             if (scientificNum[i] == '.') continue;
39 
40             cout << scientificNum[i];
41 
42             if (i - 2 == exp && i != charEPos - 1) {
43                 cout << '.';
44             }
45         }
46 
47         for (int i = 0; i < exp - charEPos + 3; ++i) {
48             cout << 0;
49         }
50     }
51 
52     //指数为负数
53     if (scientificNum[charEPos + 1] == '-') {
54         cout << "0.";
55         for (int i = 0; i < exp - 1; ++i) {
56             cout << 0;
57         }
58 
59         for (int i = 1; i < charEPos; ++i) {
60             if (scientificNum[i] != '.') {
61                 cout << scientificNum[i];
62             }
63         }
64     }
65     
66     return 0;
67 }

注意事项:

   无。

posted @ 2019-08-25 20:26  多半是条废龙  阅读(158)  评论(0)    收藏  举报