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:

  1. 3 12300 12358.9

Sample Output 1:

  1. YES 0.123*10^5

Sample Input 2:

  1. 3 120 128

Sample Output 2:

  1. NO 0.120*10^3 0.128*10^3
  2. 其实这道题的难度在于得到这个数的幂次
  1. #include <iostream>
  2. #include <string>
  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 != -)
  10. {
  11. str.erase(k, );//删除小数点
  12. if (str[] == '')
  13. {
  14. k = ;
  15. str.erase(, );//删除第一个0
  16. }
  17. }
  18. else//没有小数
  19. {
  20. if (str != "")
  21. k = str.length();
  22. else
  23. {
  24. k = ;
  25. str.erase(, );//删除第一个0
  26. }
  27. }
  28. while (!str.empty() && str[] == '')
  29. {
  30. str.erase(, );//输出前面的0
  31. k--;//比如0.000128 = 0.128*10^-3
  32. }
  33. if (str.empty())//这个数就是0
  34. k = ;
  35. while (str.length() < N)
  36. str += "";//位数不够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. else
  55. {
  56. cout << "NO ";
  57. cout << "0." << A << "*10^" << k1 << " ";
  58. cout << "0." << B << "*10^" << k2 << endl;
  59. }
  60. return ;
  61. }

PAT甲级——A1060 Are They Equal的更多相关文章

  1. PAT 甲级 1060 Are They Equal

    1060. Are They Equal (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue If a ma ...

  2. pat 甲级 1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)

    1060 Are They Equal (25 分)   If a machine can save only 3 significant digits, the float numbers 1230 ...

  4. PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)

    1053 Path of Equal Weight (30 分)   Given a non-empty tree with root R, and with weight W​i​​ assigne ...

  5. PAT甲级——A1053 Path of Equal Weight

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  6. PAT甲级1060 Are They Equal【模拟】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872 题意: 给定两个数,表示成0.xxxx ...

  7. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  9. PAT甲级题分类汇编——树

    本文为PAT甲级分类汇编系列文章. AVL树好难!(其实还好啦~) 我本来想着今天应该做不完树了,没想到电脑里有一份讲义,PPT和源代码都有,就一遍复习一遍抄码了一遍,更没想到的是编译一遍通过,再没想 ...

随机推荐

  1. VS2010-MFC(对话框:字体对话框)

    转自:http://www.jizhuomi.com/software/175.html 字体对话框的作用是用来选择字体.我们也经常能够见到.MFC使用CFontDialog类封装了字体对话框的所有操 ...

  2. service sshd start启动失败,Badly formatted port number.

    在做xhell学习的时候,把端口号修改了,后面忘记修改回 来,导致 [root@MyRoth 桌面]# service sshd start 正在启动 sshd:/etc/ssh/sshd_confi ...

  3. 创建文件夹、新建txt文件

    1.创建文件夹 QString myMkdir(QString path, QString floderName) //参数 path,创建的文件夹所在路径:  参数floderName,所创建的文件 ...

  4. Python全栈开发:json与pickle

    #!/usr/bin/env python # -*- coding;utf-8 -*- """ 正解(序列化):将Python数据类型转换成json或者pickle格式 ...

  5. Array.prototype.splice()

    splice() 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容.此方法会改变原数组. 在1位置上添加一项 我们没有下标为4的项,这个超了,就在最大项后面添加这 ...

  6. leetcode-337-打家劫舍三*

    题目描述: 方法一:递归 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): ...

  7. 【9.14NOIP模拟pj】wtaxi 题解——搜索

    [9.14NOIP模拟pj]wtaxi 题目简化 有K辆车,N个人,上车给D元,只有S分钟.上车后无论多少人都要给D元,原地等多少分钟就没了多少元.求最小花费的钱. 我的思路 毫无疑问,此题可以用搜索 ...

  8. JavaSE_12_序列化流和打印流

    1.1 序列化流概述 Java 提供了一种对象序列化的机制.用一个字节序列可以表示一个对象,该字节序列包含该对象的数据.对象的类型和对象中存储的属性等信息.字节序列写出到文件之后,相当于文件中持久保存 ...

  9. 安装Tengine和Tengine说明

    什么是Tengine 官方帮助文档:http://tengine.taobao.org/nginx_docs/cn/   Tengine的安装   新建tengine用户组 groupadd -r n ...

  10. javaweb的几种开发模式

    1.MVC模式基础 1.1.MVC模式简介 MVC是一种架构型模式,它本身并不引入新的功能,只是用来指导我们改善应用程序的架构,使得应用的模型和视图相分离,从而达到更好的开发和维护效率.在MVC模式中 ...