[PAT] Class 1061 Dating (20 points) (simulation should pay attention to details)

Meaning of the questions:

Enter a positive integer N (<= 100), followed by two input floating-point (which may contain a leading zero, PAT has been used for the input to the string, this unknown), while retaining the significant digits of N is determined whether the two numbers are equal, and the output in scientific notation.

trick:

Test point data comprising 3 significant figures after the decimal point, then the position of the decimal point should be the index minus 1 plus significant digit position.

Test Points

Code:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
string a,b;
int ans_a[107],ans_b[107];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
cin>>a>>b;
int cnt=0;
int posa=a.size();
int posb=b.size();
int staa=a.size();
int stab=b.size();
for(int i=0;i<a.size();++i)if(a[i]=='.'){
posa=i;
break;
}
for(int i=0;i<b.size();++i)if(b[i]=='.'){
posb=i;
break;
}
for(int i=0;i<a.size();++i)if(a[i]!='0'&&a[i]!='.'){
staa=i;
break;
}
for(int i=0;i<b.size();++i)if(b[i]!='0'&&b[i]!='.'){
stab=i;
break;
}
//cout<<posa<<" "<<staa<<" "<<posb<<" "<<stab<<"\n";
for(int i=staa;i<a.size();++i){
if(a[i]=='.')
continue;
if(cnt<n)
ans_a[++cnt]=a[i]-'0';
}
for(int i=cnt+1;i<=n;++i)
ans_a[i]=0;
int tot=0;
for(int i=stab;i<b.size();++i){
if(b[i]=='.')
continue;
if(tot<n)
ans_b[++tot]=b[i]-'0';
}
for(int i=tot+1;i<=n;++i)
ans_b[i]=0;
int flag=0;
for(int i=1;i<=n;++i)if(ans_a[i]!=ans_b[i])
flag=1;
if(!flag&&posa-staa==posb-stab||staa==a.size()&&stab==b.size())
cout<<"YES ";
else
cout<<"NO ";
cout<<"0.";
for(int i=1;i<=n;++i)
cout<<ans_a[i];
int ans=0;
if(posa-staa<0)
ans=posa-staa+1;
else
ans=posa-staa;
if(staa==a.size())
ans=0;
cout<<"*10^"<<ans;
if((flag||posa-staa!=posb-stab)&&(staa!=a.size()||stab!=b.size())){
cout<<" 0.";
for(int i=1;i<=n;++i)
cout<<ans_b[i];
int anss=0;
if(posa-staa<0)
anss=posb-stab+1;
else
anss=posb-stab;
if(stab==b.size())
anss=0;
cout<<"*10^"<<anss;
}
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11682024.html