原题链接:https://www.patest.cn/contests/pat-a-practise/1073
1073. Scientific Notation (20)
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 file 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-03Sample Output 1:
0.00123400Sample Input 2:
-1.2E+10Sample Output 2:
-12000000000
科学计数法转换为正常实数
主要分两种情况:1.小数点向前移:如果原先整数部分小于前移位数,则补0;如果大于,小数点移动即可
2.小数点后移:如果原先小数部分小于后移位数,则补0,如果大于,小数点移动即可
这里我把小数点前移且需要补0情况单独输出,其余情况又分为后移补0情况和不需要补0(前移和后移)情况
// pat_Scientific Notation.cpp : 定义控制台应用程序的入口点。
//
#include <cstdio>
#include <cstring>
#include <cmath>
char str[10010];
int main(int argc, char* argv[])
{
int i, j, len, flag = 0, cc = 0, tmp = 0, cnt = 0,sum=0;//cnt统计小数点前面位数,cc统计小数点后面位数,sum记录指数大小,tmp判断指数正负
scanf("%s", str);
len = strlen(str);
for (i = 1; i < len; i++)
{
if (str[i] == '.'){
flag = 1;
continue;
}
if (!flag)
cnt++;
if (flag)
{
cc++;
if (str[i] == 'E')
{
if (str[i + 1] == '+')
tmp = 1;
sum = 0;
for (j = i + 2; j < len; j++)
{
sum = sum * 10 + str[j] - '0';
}
break;
}
}
}
cc--;
if (str[0] == '-')
printf("-");
if (!tmp&&cnt <= sum)
{
printf("0.");
for (i = 1; i <= sum - cnt; i++)
printf("0");
for (i = 1; str[i] != 'E'; i++){
if (str[i] == '.')
continue;
printf("%c", str[i]);
}
printf("\n");
}
else
{
int head;
if (tmp)
head = sum + cnt;
else
head = sum - cnt;
for (i = 1; i <= head&&i < len&&str[i] != 'E'; i++){
if (str[i] == '.')
{
head++;
continue;
}
printf("%c", str[i]);
}
if (tmp&&sum >= cc)
{
for (i = 1; i <= sum - cc; i++)
printf("0");
printf("\n");
return 0;
}
else
{
printf(".");
}
for (i = head + 1; str[i] != 'E'; i++)
printf("%c", str[i]);
printf("\n");
}
return 0;
}