【PAT1060】Are They Equal
2013-11-17 22:50
501 查看
1060. Are They Equal (25)
时间限制
50 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
题意:将两个float型数据使用科学计数法表示,并比较两者是否相同
关键:要考虑所给输入数据的多种情况,比如 0 , 0.0,0.0123,05.032,00.020等这种比较特殊的情况。
代码如下:
时间限制
50 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.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
题意:将两个float型数据使用科学计数法表示,并比较两者是否相同
关键:要考虑所给输入数据的多种情况,比如 0 , 0.0,0.0123,05.032,00.020等这种比较特殊的情况。
代码如下:
#include <iostream> #include <fstream> #include <cstring> using namespace std; ifstream fin("in.txt"); #define cin fin int convert(const char* c,int n,char* &res) { int point,count; point = count = 0; int i=0,j=0; while(c[i]=='0')i++; //先略去最前面的'0' if(c[i]=='.') //形式为 (0)*.## { bool isBegin = false; while(c[i]!='\0') { if(c[i]=='.') { point = 1; }else if(c[i]!='0') { isBegin = true; } if(isBegin){ if(count<n) { res[count]=c[i]; count++; } }else { point--; } i++; } if(!isBegin)point=0; }else //形式为 (0)*#.## { j=i; while(c[i]!='\0') { if(c[i]=='.') { point = i-j; }else { if(count<n) { res[count]=c[i]; count++; } } i++; } if(point==0)//如果没有. 则等于数字个数(去掉前面的0) point = i-j; } if(count<n) //如果数字个数不足,补'0' { i = count; while(i<n) { res[i]='0'; i++; } } res ='\0'; return point; } int main() { int n; cin>>n; char A[105],B[105]; cin>>A>>B; int Ac,Bc; char *Ares,*Bres; Ares = new char[n+1]; Bres = new char[n+1]; Ac = convert(A,n,Ares); Bc = convert(B,n,Bres); if(Ac==Bc && strcmp(Ares,Bres)==0) { cout<<"YES 0."<<Ares<<"*10^"<<Ac<<endl; }else { cout<<"NO 0."<<Ares<<"*10^"<<Ac<<" 0."<<Bres<<"*10^"<<Bc<<endl; } system("PAUSE"); return 0; }
相关文章推荐
- OpenCV实现的高斯滤波探究_1(《学习OpenCV》练习题第五章第三题ab部分)
- ARC的处理界限,至_bridge的衍生用法
- 关于位域的字节内存储顺序、字节对齐、字节序以及符号
- Android通过for循环批量发送短信
- IOS7手势识别
- hdu 4782 Beautiful Soupz
- winrar自解压释放路径详解
- 利用VC调用WinRAR解压和压缩文件
- Spring3.0 注解解释
- (程序员面试题)字符串处理之寻找最大不重复子串
- openwrt 编程器固件的制作方法
- PHP爱好者请坚定你们的信念!
- 安卓系统机顶盒中如何自由移动程序的焦点(没测试过)
- postconf 命令常用参数
- uva 10026 Shoemaker's Problem
- css优先权问题
- SWT/JFace开发遇到org.eclipse.core.runtime.IProgressMonitor问题的解决办法(转载)
- getOutputStream() 的问题
- C-常用字符串操作函数详解
- 经典设计:17个最有效的学习着陆页设计的例子