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×10
5
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 10
100
, 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.12010^3 0.12810^3
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
#include<iostream>
#include<set>
#include<string>
using namespace std;
string deal(string num,int &e,int n){
string s;
while(num[0]=='0'&&num.length()>0) num.erase(num.begin()); //坑点000001.234前多余0,同时找小数点
if(num[0]=='.'){
int i=1;
while(num[i++]=='0') e--;
num.erase(num.begin(),num.begin()-e+1); //小数点和小数点后直接的0删除;
if(num.length()==0) e=0; //坑点0.00000后全是0
}
else{
for(int i=0;i<num.length();i++)
{
if(num[i]!='.') e++; //坑点 小数
else
{
num.erase(num.begin()+i); //do not forget the "begin";
break;
}
}
}
for(int i=0;i<n;i++){
if(num[i]) //坑点,位数不够n;
s+=num[i];
else s+='0';
}
s.insert(0,"0.");
return s;
}
int main(){
string a,b;int n,e1=0,e2=0;
cin>>n>>a>>b;
string s1=deal(a,e1,n),s2=deal(b,e2,n);
if(s1==s2&&e1==e2)
cout<<"YES"<<" "<<s1<<"*10^"<<e1;
else cout<<"NO"<<" "<<s1<<"*10^"<<e1<<" "<<s2<<"*10^"<<e2;
return 0;
}

浙公网安备 33010602011771号