1060 Are They Equal (25 分)
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×105 with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.
Input Specification:
Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10100, and that its total digit number is less than 100.
Output Specification:
For each test case, print in a line YES
if the two numbers are treated equal, and then the number in the standard form 0.d[1]...d[N]*10^k
(d[1]
>0 unless the number is 0); or NO
if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.
Note: Simple chopping is assumed without rounding.
Sample Input 1:
3 12300 12358.9
Sample Output 1:
YES 0.123*10^5
Sample Input 2:
3 120 128
Sample Output 2:
NO 0.120*10^3 0.128*10^3
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
此题太烦,写法太烂
#include<iostream>
#include<string>
using namespace std;
int N,p1,p2;
//是不是0
bool isZreo(string str){
for(int i=0;i<str.length();i++){
if(str[i]!='0'&&str[i]!='.'){
return false;
}
}
return true;
}
//删除开头的0
void clearZore(string& str){
if(isZreo(str)) return;//0不处理
//小数点前面至少一位
while(str.length()>1&&(*(str.begin()+1)!='.')){//最后一个即是0也不能删除
if((*str.begin())=='0') {
str.erase(str.begin());
}else{
break;
}
}
}
//求位数 并删除小数点
int len(string& str){
if(isZreo(str)){
str="";
for(int i=0;i<N;i++){
str=str+'0';
}
return 0;
}
//0.XX或者0.00XX
if(str.length()>2&&str[0]=='0'&&str[1]=='.'){//注意=='0'而不是=='.'
str.erase(0,2);//删除"0."
int n=0;
while(*(str.begin())=='0'){//注意是=='0' 而不是==0
str.erase(str.begin());
n--;
}
return n;//负次幂
}
int n=str.find(".");
if(n!=string::npos){
str.erase(n,1);//删除小数点
return n;
}else{
return str.length();//没有小数点 直接就是返回长度
}
}
void addZero(string& str){
for(int i=0;i<N-str.length();i++){
str=str+'0';
}
}
int main(){
string s1,s2,sa,sb;
while(cin>>N){
cin>>s1>>s2;
clearZore(s1);
clearZore(s2);
p1=len(s1);
p2=len(s2);
sa=s1.substr(0,N);
sb=s2.substr(0,N);
addZero(sa);
addZero(sb);
if(sa==sb&&p1==p2){
cout<<"YES "<<"0."<<sa<<"*"<<"10^"<<p1<<endl;
}else{
cout<<"NO "<<"0."<<sa<<"*"<<"10^"<<p1;
cout<<" 0."<<sb<<"*"<<"10^"<<p2<<endl;
}
}
return 0;
}
根据测试数据慢慢改的。。。