PTA乙级 (*1024 科学计数法 (20分))
1024 科学计数法 (20分)
https://pintia.cn/problem-sets/994805260223102976/problems/994805297229447168
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
string str;
cin>>str;
int i=0,j;
while(str[i]!='E') i++;
string t=str.substr(1,i-1);
int n=stoi(str.substr(i+1));
if(str[0]=='-') cout<<"-";
if(n<0)
{
cout<<"0.";
for(j=0;j<abs(n)-1;j++) cout<<"0";
for(j=0;j<t.length();j++)
if(t[j]!='.') cout<<t[j];
}
else{
int cnt=0;
cout<<t[0];
for(j=2,cnt=0;j<t.length()&&cnt<n;cnt++,j++) 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;
}
分析:n保存E后面的字符串所对应的数字,t保存E前面的字符串,不包括符号位。当n<0时表示向前移动,那么先输出0. 然后输出abs(n)-1个0,然后继续输出t中的所有数字;当n>0时候表示向后移动,那么先输出第一个字符,然后将t中尽可能输出n个字符,如果t已经输出到最后一个字符(j == t.length())那么就在后面补n-cnt个0,否则就补充一个小数点. 然后继续输出t剩余的没有输出的字符.
天晴了,起飞吧

浙公网安备 33010602011771号