标签:regular cin with trail mes pac lse amp ati
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.
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.
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.
+1.23400E-03
0.00123400
-1.2E+10
-12000000000
题意:
将数字由科学计数法表示改为普通数字表示方法。
思路:
模拟。
Code:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int main() { 6 string in; 7 cin >> in; 8 if (in[0] == ‘-‘) cout << ‘-‘; 9 int pos = in.find(‘E‘); 10 string coef = in.substr(1, pos - 1); 11 string exp = in.substr(pos + 2); 12 int e = stoi(exp); 13 int c = stoi(coef); 14 int len = coef.length(); 15 if (in[pos + 1] == ‘+‘) { 16 if (len - 2 > e) { 17 cout << coef[0]; 18 for (int i = 2; i < len; ++i) { 19 if (i == e + 2) cout << ‘.‘; 20 cout << coef[i]; 21 } 22 cout << endl; 23 } else { 24 cout << coef[0]; 25 for (int i = 2; i < len; ++i) cout << coef[i]; 26 for (int i = 0; i < e - len + 2; ++i) cout << ‘0‘; 27 } 28 } else { 29 if (e == 0) cout << 1 << endl; 30 for (int i = 0; i < e; ++i) { 31 cout << ‘0‘; 32 if (i == 0) cout << ‘.‘; 33 } 34 cout << coef[0]; 35 for (int i = 2; i < len; ++i) cout << coef[i]; 36 cout << endl; 37 } 38 return 0; 39 }
标签:regular cin with trail mes pac lse amp ati
原文地址:https://www.cnblogs.com/ruruozhenhao/p/12819836.html