博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1060 Are They Equal
阅读量:4540 次
发布时间:2019-06-08

本文共 2627 字,大约阅读时间需要 8 分钟。

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 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 (<) 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 1, 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 其实这道题的难度在于得到这个数的幂次
1 #include 
2 #include
3 using namespace std; 4 string A, B;//注意,10^100超出double的范围,只能使用字符串来存取 5 int N; 6 int dealString(string &str)//数据预处理,返回数据位数 7 { 8 int k = str.find('.');//找到小数点 9 if (k != -1)10 {11 str.erase(k, 1);//删除小数点12 if (str[0] == '0')13 {14 k = 0;15 str.erase(0, 1);//删除第一个016 }17 }18 else//没有小数19 {20 if (str != "0")21 k = str.length();22 else23 {24 k = 0;25 str.erase(0, 1);//删除第一个026 }27 }28 while (!str.empty() && str[0] == '0')29 {30 str.erase(0, 1);//输出前面的031 k--;//比如0.000128 = 0.128*10^-332 }33 if (str.empty())//这个数就是034 k = 0;35 while (str.length() < N)36 str += "0";//位数不够0来凑37 return k;38 }39 40 int main()41 {42 cin >> N >> A >> B;43 //使用k1,k2来得到A,B的位数44 int k1, k2;45 k1 = dealString(A);46 k2 = dealString(B);47 A.assign(A.begin(), A.begin() + N);//取前N位48 B.assign(B.begin(), B.begin() + N);49 if (A == B && k1 == k2)50 {51 cout << "YES ";52 cout << "0." << A << "*10^" << k1 << endl;53 }54 else55 {56 cout << "NO ";57 cout << "0." << A << "*10^" << k1 << " ";58 cout << "0." << B << "*10^" << k2 << endl;59 }60 return 0; 61 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11294212.html

你可能感兴趣的文章
[国家集训队2012]middle
查看>>
使用Holer远程桌面登录家里电脑和公司内网电脑
查看>>
Java数组5作业(2015-8-27)
查看>>
Nginx事件管理之epoll模块
查看>>
idea集成 MyBatis Generator 插件,自动生成dao,model,sql map文件
查看>>
用数据告诉你关于手机app的15个有趣事实
查看>>
BBC英语-adverbs of frequency
查看>>
python中的List,Tuple,Set,Dictionary
查看>>
JavaWeb 学习007-4个页面,5条sql语句(添加、查看、修改、删除)2016-12-2
查看>>
用JavaScript 来将数字转换成字符。
查看>>
扩展欧几里得算法
查看>>
java中的包装类
查看>>
采用多种算法,模拟摇奖:从1-36中随机抽出8个不重复的数字
查看>>
sp2.1 Practical aspects of Deep Learning
查看>>
java中的缓存
查看>>
2、文件夹
查看>>
DLL_Vs_CLL
查看>>
MakeFile
查看>>
AVAST 4.8
查看>>
6.1.1 web前端介绍
查看>>