关于string的练习题目

时间:2022-06-28 19:45:25

/*Are they equal*/
#include<iostream>
#include<string>
using namespace std;
int n;
string deal(string s, int & e){
int k = 0;
while(s.length() > 0 && s[0] == '0'){
s.erase(s.begin());
}
if (s[0] == '.'){
s.erase(s.begin());//擦除小数点
while(s.length() > 0 && s[0] == '0'){//擦除非零位前的所有零
s.erase(s.begin());
e--;
}
}
else{
while (k < s.length()&& s[k] != '.'){
k++;
e++;
}
if(k <s.length()){
s.erase(s.begin() + k);
}
}
if (s.length() == 0){
e == 0;
}
int num = 0;
k = 0;
string res;
while (num < n){
if (k < s.length()) res += s[k++];
else res += "0";
num++;
}
return res;
}
int main(){
string s1, s2, s3, s4;
cin >> n >> s1 >> s2;
int e1 = 0, e2 = 0;
s3 = deal (s1, e1);
s4 = deal (s2, e2);
if(s3 == s4 && e1 == e2){
cout<<"Yes 0."<<s3<<"*10^"<<e1<<endl;
}else{
cout<<"No 0."<<s3<<"*10^"<<e1<<" 0."<<s4<<"*10^"<<e2<<endl;
}
return 0;
}

输入样例:

3 12300 12358.9

输出样例:

Yes 0.123*10^5