PAT (Advanced Level) 1060 Are They Equal
作者:互联网
题解
找出有效的字符串(t),第一个非零数字的位置(zero)和小数点的位置(point)。
若 s = "0012.104654",N = 4 则 t = "12104654",zero = 2,point = 4,所以答案为 0.1210*10^(point-zero)
若 s = "0000.00010465",N = 4 则 t = "10465",zero = 8,point = 4,所以答案为 0.1046*10^(point-zero+1)
注意 s = "0.0000000"的情况,若 N = 3,则答案为0.000*10^0
代码
#include<bits/stdc++.h> using namespace std; string process(int N,string s,int & k); int main() { int i,N,k1,k2; string str1,str2; cin>>N>>str1>>str2; str1=process(N,str1,k1); str2=process(N,str2,k2); if(str1==str2 && k1==k2) printf("YES %s*10^%d",str1.c_str(),k1); else printf("NO %s*10^%d %s*10^%d",str1.c_str(),k1,str2.c_str(),k2); system("pause"); return 0; } string process(int N,string s,int & k) { int i,zero,point; string t; zero=point=-1; for(i=0;i<s.size();i++) { if(s[i]=='.') point=i; else if(s[i]=='0' && zero==-1) continue; else { if(zero==-1) zero=i; t+=s[i]; } } if(zero==-1) { t.insert(0,N,'0'); k=0; return "0."+t; } else { if(point==-1) point=i; if(t.size()<N) t.insert(t.size(),t.size()-N,'0'); if(zero>point) k=point-zero+1; else k=point-zero; return "0."+t.substr(0,N); } }
标签:10,PAT,string,point,int,1060,str1,Level,zero 来源: https://www.cnblogs.com/VividBinGo/p/12233093.html