PAT (Basic Level) Practice 1024 科学计数法 分数 20

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9].[0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部分的正负号即使对正数也必定明确给出。

现以科学计数法的格式给出实数 A,请编写程序按普通数字表示法输出 A,并保证所有有效位都被保留。

输入格式:

每个输入包含 1 个测试用例,即一个以科学计数法表示的实数 A。该数字的存储长度不超过 9999 字节,且其指数的绝对值不超过 9999。

输出格式:

对每个测试用例,在一行中按普通数字表示法输出 A,并保证所有有效位都被保留,包括末尾的 0。

输入样例 1:

+1.23400E-03
 

输出样例 1:

0.00123400
 

输入样例 2:

-1.2E+10
 

输出样例 2:

-12000000000
 
代码长度限制
16 KB
时间限制
200 ms
内存限制
64 MB
 

解题:

#include<stdio.h>
#include<string.h>

int main()
{
int i,j=0,h=0,d,third=0;
char a,b,scend[1001];
a=getchar();
scanf("%d.%[0-9]E",&d,scend);
/*%[] 的意思是:读入此集合所限定的那些字符。
例如 %[A-Z] 是指接受大写字母,一旦遇到非大写字母便停止接受,
而 %[^] 是指不要读入此集合所限定的那些字符。
例如 % [^A-Z] 是指不接受大写字母,一旦遇到大写字母便停止接受*/
b=getchar();
scanf("%d",&third);
//printf("%c %d %s %c %d\n",a,d,scend,b,third);
if(a=='-')
{
printf("-");
}
if(b=='-')
{
printf("0.");
for(i=third-1;i>0;i--)
{
printf("0");
}
printf("%d",d);
for(i=0;scend[i]!='\0';i++)
{
printf("%d",scend[i]-'0');
}
}
else if(b=='+')
{
if(d>0)
{
printf("%d",d);
for(i=0;scend[i]!='\0';i++)
{
j++;
}
if(j<=third)
{
for(i=0;scend[i]!='\0';i++)
{
printf("%d",scend[i]-'0');
}
for(i=third-j;i>0;i--)
{
printf("0");
}
}
else
{
for(i=0;i<third;i++)
{
printf("%d",scend[i]-'0');
}
printf(".");
for(i=third;scend[i]!='\0';i++)
{
printf("%d",scend[i]-'0');
}
}
}
if(d==0)
{
for(i=0;scend[i]=='0';i++)
{
h++;
}
for(i=0;scend[i]!='\0';i++)
{
j++;
}
if(j<third)
{
for(i=j;scend[i]!='\0';i++)
{
printf("%d",scend[i]-'0');
}
for(i=third-j;i>0;i--)
{
printf("0");
}
}
else
{
for(i=third-h;i<third;i++)
{
printf("%d",scend[i]-'0');
}
if(scend[third]!='\0')
{
printf(".");
for(i=third;scend[i]!='\0';i++)
{
printf("%d",scend[i]-'0');
}
}
}
}
}
}
/*
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
int i = 0;
while (s[i] != 'E') i++;
string t = s.substr(1, i-1);
int n = stoi(s.substr(i+1));
if (s[0] == '-') cout << "-";
if (n < 0) {
cout << "0.";
for (int j = 0; j < abs(n) - 1; j++) cout << '0';
for (int j = 0; j < t.length(); j++)
if (t[j] != '.') cout << t[j];
} else {
cout << t[0];
int cnt, j;
for (j = 2, cnt = 0; j < t.length() && cnt < n; j++, cnt++) cout << t[j];
if (j == t.length()) {
for (int k = 0; k < n - cnt; k++) cout << '0';
} else {
cout << '.';
for (int k = j; k < t.length(); k++) cout << t[k];
}
}
return 0;
}*/

 

posted @ 2022-09-22 14:56  slowlydance2me  阅读(18)  评论(0编辑  收藏  举报