写了个简单的代码,得分17/25 。
满分代码: 需要考虑好多的东西
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
int n; //有效位数
string deal(string s, int& e){
int k = 0;
//去掉s的前导0
while(s.length() > 0 && s[0] == '0'){
s.erase(s.begin());
}
//若去掉前导零后是小数点,则说明s是小于1的小数
if(s[0] == '.'){
//去掉小数点
s.erase(s.begin());
while(s.length() > 0 && s[0] == '0'){
s.erase(s.begin()); //去掉小数点后非零位前的所有零
e--;//每去掉一个0,指数e减1
}
} else { //若去掉前导零后不是小数点,则找后面的小数点并将其删除
while(k < s.length() && s[k] != '.'){ //寻找小数点
k++;
e++; //只要不是小数点,则e++
}
if(k < s.length()){ //当while循环结束后,k<s.length(),说明遇到了小数点
s.erase(s.begin() + k);//删除小数点
}
}
if(s.length() == 0){
e = 0; //如果去掉前导零后s的长度变为0,则说明这个数为0
}
int num = 0;
k=0;
string res;
while(num < n){ //只要精度还没有到n
if(k < s.length()) res += s[k++]; //只要还有数字,就加到res末尾
else res+= '0';//否则res末尾添加零
num++; //精度+1
}
return res;
}
int main(int argc, char** argv) {
cin >> n;
string str1, str2;
cin >> str1 >> str2;
int e1 = 0, e2 = 0;
string str11 = deal(str1, e1);
string str22 = deal(str2, e2);
if(str11 == str22 && e1 == e2){
cout << "YES 0." << str11 << "*10^" << e1;
} else {
cout << "NO 0." << str11 << "*10^" << e1
<< " 0." << str22 << "*10^" << e2;
}
return 0;
}