• 发表于 10个月前
• 阅读 2
• 收藏 0
• 评论 0

https://www.patest.cn/contests/pat-a-practise/1073

Sample Input 1:

+1.23400E-03

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;

int main(int argc, char *argv[]) {
char str[10010];
scanf("%s", str);
int len = strlen(str);
if (str[0] == '-') printf("-");
int pos = 0; //pos存放字符串中E的位置
while (str[pos] != 'E')
pos++;

int exp = 0;	//指数
for (int i = pos + 2; i < len; i++)
exp = exp * 10 + str[i] - '0';

if (exp == 0)	//指数为0输出即可
for (int i = 1; i < pos; i++)
printf("%c", str[i]);

if (str[pos + 1] == '-') {
printf("0.");
for (int i = 0; i < exp - 1; i++)
printf("0");
printf("%c", str[1]);  //输出除了小数点以外的数字
for (int i = 3; i < pos; i++)
printf("%c", str[i]);
} else {
for (int i = 1; i < pos; i++) {//输出小数点移动后的数
if (str[i] == '.') continue;
printf("%c", str[i]);

//小数点加在位置exp+2上原小数点和E之间的数字个数pos-3不能等于小数点右移位数exp
if (i == exp + 2 && pos - 3 != exp)
printf(".");
}

//如果指数较大,输出多于的0
for (int i = 0; i < exp - (pos - 3); i++)
printf("0");
}
return 0;
}

×