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.d1...dN*10^k" (d1>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.9Sample Output 1:
YES 0.123*10^5Sample Input 2:
3 120 128Sample Output 2:
NO 0.120*10^3 0.128*10^3
思路:题目不难,但是要AC很难,要考虑的情况比较多
几个特殊的测试点:
(1)3 0 0.0000000
(2)3 0 0
(3)3 0123 00000123
注意输入0的时候要特别注意 若有效位为3 则输出应该是0.000*10^0 这样的格式,不是0.00*10^0这样
参考AC代码:
#include <iostream>
#include <string.h>
using namespace std;
string standardIt(string s){ //把输入的浮点数转化成标准的格式
if(s[0]=='.'){
s = "0"+s;
return s;
}
int index = 1;
if(s[0]=='0' && s[1]!='.'){
while(s[index]=='0'){
if(s[index+1]=='.'){
break;
}
index++;
}
if(index==s.size()){
return "0";
}
}else{
return s;
}
s = s.substr(index);
return s;
}
string int2str(int t){ //把整形转化成字符串整形
string s="";
while(t%10!=0){
char ch = '0'+t%10;
s = ch+s;
t /= 10;
}
return s;
}
string changeFormat(string s,int n){ //把输入的字符串浮点数转化成要输出的标准格式,再比较
int temp = 0; //记录小数点在字符串中的位置
string result = "";
if(s=="0"){ //输入为 0 的特殊情况
result +="0.";
for(int i=0;i<n;i++){
result += '0';
}
result += "*10^0";
return result;
}
for(int i=0;i<s.size();i++){
if(s[i]!='.'){
temp++;
}else{
if(temp==1 && s[0]=='0'){ //输入的数小于1
int indexZero = 0;
for(int j=2;j<s.size();j++){ //小于0时找到第一个非0数值
if(s[j]!='0'){
indexZero = j;
break;
}
}
if(indexZero==0){ //输入为0.00000000的情况
result +="0.";
for(int i=0;i<n;i++){
result += '0';
}
result += "*10^0";
return result;
}
if(s.size()-indexZero<n){ //非0的个数比有效数个数少 需要补0
result += "0.";
for(int j=indexZero;j<s.size();j++){
result += s[j];
}
for(int j=0;j<n-s.size()+indexZero;j++){
result += '0';
}
if(indexZero-2==0){
result += "*10^0";
return result;
}else{
result += "*10^-";
string tmp = int2str(indexZero-2);
result += tmp;
return result;
}
}else{ //非0个数比有效数多
result += "0.";
for(int j=indexZero;j<indexZero+n;j++){
result += s[j];
}
if(indexZero-2==0){
return result;
}else{
result += "*10^-";
string tmp = int2str(indexZero-2);
result += tmp;
return result;
}
}
}else{ //输入佛如值>0
if(temp>n){
result += "0.";
for(int j=0;j<n;j++){
result += s[j];
}
result += "*10^";
string tmp = int2str(temp);
result += tmp;
return result;
}else{
int afterPoint = n-temp;
result += "0.";
for(int j=0;j<temp;j++){
result += s[j];
}
for(int j=temp+1;j<temp+1+afterPoint;j++){
result += s[j];
}
result += "*10^";
string tmp = int2str(temp);
result += tmp;
return result;
}
}
}
}
result += "0.";
if(s.size()>=n){ //输入为整数且位数>有效位数
for(int i=0;i<n;i++){
result +=s[i];
}
result += "*10^";
string tmp = int2str(s.size());
result += tmp;
return result;
}else{ //输入为整数且位数<有效位数
int afterPoint = n-s.size();
for(int i=0;i<s.size();i++){
result += s[i];
}
for(int i=0;i<afterPoint;i++){
result += '0';
}
result += "*10^";
string tmp = int2str(n);
result += tmp;
return result;
}
}
int main()
{
int N;
string s1,s2;
cin>>N>>s1>>s2;
s1 = standardIt(s1);
s2 = standardIt(s2);
if(changeFormat(s1,N) == changeFormat(s2,N)){
cout<<"YES "<<changeFormat(s1,N);
}else{
cout<<"NO "<<changeFormat(s1,N)<<" "<<changeFormat(s2,N);
}
return 0;
}
本文探讨了在计算机中,当浮点数保存到只有3位有效数字时,如何判断两个浮点数是否被视为相等。通过输入指定的位数、两个浮点数,并输出它们是否等价以及标准形式,本文深入分析了简单截断而非四舍五入的处理方式,同时考虑了特殊测试点,如输入0和极大值等。
2835

被折叠的 条评论
为什么被折叠?



