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
字符串模拟,需要分类讨论各种情况:
1)指数为正
a)指数大于等于小数位数
后面可能要补零
b)指数小于小数位数
注意原数个位为零和不为零的情况不同。
2)指数为负
需要补零
3)指数为零
直接提取输出
- #include
- #include
- using namespace std;
-
- int main() {
- string a, b = "";
- int ex = 0, flag, pos;
- cin >> a;
- if (a[0] == '-') {
- cout << '-';
- }
- for (int i = 1; i < a.size(); i++) {
- if (a[i] == 'E') {
- pos = i;
- if (a[i + 1] == '+') {
- flag = 1;
- } else if (a[i + 1] == '-') {
- flag = -1;
- }
- for (int j = i + 2; j < a.size(); j++) {
- ex = ex * 10 + a[j] - '0';
- }
- break;
- }
- }
- if (ex == 0) {
- flag = 0;
- }
- if (flag == 1) {
- int num = pos - 3; //统计小数点后有几位
- if (num <= ex) {
- for (int i = 1; i < pos; i++) {
- if (a[i] != '.') {
- cout << a[i];
- }
- }
- for (int i = 0; i < ex - num; i++) {
- cout << 0;
- }
- } else {
- int t = 0;
- if (a[1] != '0') {
- b += a[1];
- }
- for (int i = 3; i < pos; i++) {
- b += a[i];
- t++;
- if (t == ex) {
- b += '.';
- }
- }
- bool f = 0;
- for (int i = 0; i < b.size(); i++) {
- if (b[i] == '0' && b[i + 1] == '.' || b[i] != '0') {
- f = 1;
- }
- if (f == 1) {
- cout << b[i];
- }
- }
- }
- } else if (flag == -1) {
- cout << "0.";
- for (int i = 1; i < ex; i++) {
- cout << 0;
- }
- for (int i = 1; i < pos; i++) {
- if (a[i] != '.') {
- cout << a[i];
- }
- }
- } else if (flag == 0) {
- for (int i = 1; i < pos; i++) {
- cout << a[i];
- }
- }
- return 0;
- }