1073 Scientific Notation (20point(s)) Easy only once

基本思想:

和示例思想相同,典型的字符串处理,注意即可,没啥新意;

关键点:

字符串问题;

 1 #include<iostream>
 2 #include<stdlib.h>
 3 #include<stdio.h>
 4 #include<vector> 
 5 #include<string>
 6 #include<math.h>
 7 #include<algorithm>
 8 using namespace std;
 9 using std::vector;
10 
11 
12 int main() {
13     string s;
14     cin >> s;
15     string n, e;
16     int index;
17     for (int i = 1; i < s.size(); i++) {
18         if (s[i] == 'E') {
19             index = i;
20             break;
21         }
22     }
23     n = s.substr(1, index-1);
24     e = s.substr(index + 2, s.size() - index - 2);
25     n.erase(n.begin() + 1);
26     int ne = 0;
27     for (int i = 0; i < e.size(); i++) {
28         ne += int(pow(10, e.size() - i - 1)*(e[i] - '0'));
29     }
30     //cout << ne << endl;
31     //cout << n << endl;
32     if (s[index + 1] == '-')
33         ne = -ne;
34     if (s[0] == '-')
35         printf("-");
36     if (ne<= 0) {
37         ne++;
38         cout << "0.";
39         while (ne < 0) {
40             ne++;
41             cout << "0";
42         }
43         cout << n;
44     }
45     else{
46         ne--;
47         //cout << "->" << ne << endl;
48         if (ne < n.size()-1) {
49             for (int i = 0; i < n.size(); i++) {
50                 if (i == ne+2)
51                     cout << ".";
52                 cout << n[i];
53             }
54         }
55         else {
56             cout << n;
57             while (ne > 0) {
58                 cout << "0";
59                 ne--;
60             }
61         }
62     }
63     //cout << n << endl << e << endl;
64     system("pause");
65     return 0;
66 }

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12200419.html