#include<bits/stdc++.h>
using namespace std;
using gg = long long;
string get_valid_number(string s_, gg valid_num){
string res;
//删除s中的小数点,以免取有效数字的时候取入小数点。
string s = s_;
gg point_index = s.find_first_of(".");
s.erase(point_index, 1);
//完成。开始工作:
gg first_index = s.find_first_not_of("0");
if(first_index == -1) res = string(valid_num, '0');
else{
res = (first_index + (valid_num-1) <= s.size()-1) ?
s.substr(first_index, valid_num) : s.substr(first_index, s.size()-first_index)
+ string(valid_num-(s.size()-first_index), '0');
}
return res;
}
gg get_dim(string s){
//做同样的判断,防止输入为空。
gg first_index = s.find_first_not_of(".0");
if(first_index == -1) return 0;
//正式开始处理。
string point{"."};
string numbers{"123456789"};
gg i = s.find_first_of(numbers);
gg num = s.find_first_of(point);
if(num-i < 0) return (num-i+1);
else return num-i;
}
int main(){
pair<string, gg> item1, item2;
gg valid_num;
double f1, f2;
cin >> valid_num >> f1 >> f2;
string s1, s2;
s1 = to_string(f1);
s2 = to_string(f2);
item1.first = get_valid_number(s1, valid_num);
item2.first = get_valid_number(s2, valid_num);
item1.second = get_dim(s1);
item2.second = get_dim(s2);
if(item1 == item2) cout << "YES 0." << item1.first << "*10^" << item1.second << endl;
else cout << "NO 0." << item1.first << "*10^" << item1.second << " 0." <<
item2.first << "*10^" << item2.second << endl;
}
错误样例:
4 0.00001230 000001230
AC代码:
#include<bits/stdc++.h>
using namespace std;
using gg = long long;
string get_valid_number(string s_, gg valid_num){
string res;
//删除s中的小数点,以免取有效数字的时候取入小数点。
string s = s_;
gg point_index = s.find_first_of(".");
if(point_index != -1) s.erase(point_index, 1);
//完成。开始工作:
gg first_index = s.find_first_not_of("0");
if(first_index == -1) res = string(valid_num, '0');
else{
res = (first_index + (valid_num-1) <= s.size()-1) ?
s.substr(first_index, valid_num) : s.substr(first_index, s.size()-first_index)
+ string(valid_num-(s.size()-first_index), '0');
}
return res;
}
gg get_dim(string s){
//做同样的判断,防止输入为空。
gg first_index = s.find_first_not_of(".0");
if(first_index == -1) return 0;
//正式开始处理。
string point{"."};
string numbers{"123456789"};
gg i = s.find_first_of(numbers);
gg num = s.find_first_of(point);
if(num == -1) return s.size();
if(num-i < 0) return (num-i+1);
else return num-i;
}
int main(){
pair<string, gg> item1, item2;
gg valid_num;
string s1, s2;
cin >> valid_num >> s1 >> s2;
item1.first = get_valid_number(s1, valid_num);
item2.first = get_valid_number(s2, valid_num);
item1.second = get_dim(s1);
item2.second = get_dim(s2);
if(item1 == item2) cout << "YES 0." << item1.first << "*10^" << item1.second << endl;
else cout << "NO 0." << item1.first << "*10^" << item1.second << " 0." <<
item2.first << "*10^" << item2.second << endl;
}