PAT甲级 Are They Equal (25) (恶心模拟)

男娘i 2021-12-03 11:04 291阅读 0赞

Are They Equal (25)

时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB

题目描述

  1. 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.

输入描述:

  1. 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.

输出描述:

  1. 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.
  2. Note: Simple chopping is assumed without rounding.

输入例子:

  1. 3 12300 12358.9

输出例子:

  1. YES 0.123*10^5
  2. 题解:
  3.   这个题关键不在于各种分情况讨论,而是如何找到有效数位
  4. 1 #include<iostream>
  5. 2 #include<cstdio>
  6. 3 #include<cstring>
  7. 4 #include<algorithm>
  8. 5 using namespace std;
  9. 6 int n;
  10. 7 string s1,s2;
  11. 8 struct node{
  12. 9 string ss;
  13. 10 int kk;
  14. 11 }e;
  15. 12 node judge(string s)
  16. 13 {
  17. 14 int flag=0;
  18. 15 int len=s.length();
  19. 16 int k=0;
  20. 17 string ans="";
  21. 18 for(int i=0;i<len;i++)
  22. 19 {
  23. 20 if(s[i]=='.')
  24. 21 flag=1;
  25. 22 else
  26. 23 {
  27. 24 if(!flag)
  28. 25 k++;
  29. 26 if(ans==""&&s[i]=='0')
  30. 27 {
  31. 28 k--;
  32. 29 }
  33. 30 else
  34. 31 {
  35. 32 ans+=s[i];
  36. 33 }
  37. 34 }
  38. 35
  39. 36 }
  40. 37 while(ans.length()<n)
  41. 38 ans+='0';
  42. 39 if(ans.length()>n)
  43. 40 ans=ans.substr(0,n);
  44. 41 // cout<<"ans="<<ans<<endl;
  45. 42 int ok=0;
  46. 43 for(int i=0;i<ans.length();i++)
  47. 44 {
  48. 45 if(ans[i]!='0')
  49. 46 {
  50. 47 ok=1;
  51. 48 break;
  52. 49 }
  53. 50 }
  54. 51 if(!ok)
  55. 52 k=0;
  56. 53 e.ss=ans;
  57. 54 e.kk=k;
  58. 55 return e;
  59. 56 }
  60. 57 int main()
  61. 58 {
  62. 59 cin>>n>>s1>>s2;
  63. 60 node ans1=judge(s1);
  64. 61 node ans2=judge(s2);
  65. 62 if(ans1.ss==ans2.ss&&ans1.kk==ans2.kk)
  66. 63 {
  67. 64 cout<<"YES "<<"0."<<ans1.ss<<"*10^"<<ans1.kk<<endl;
  68. 65 }
  69. 66 else
  70. 67 {
  71. 68 cout<<"NO "<<"0."<<ans1.ss<<"*10^"<<ans1.kk<<" "<<"0."<<ans2.ss<<"*10^"<<ans2.kk<<endl;
  72. 69 }
  73. 70 return 0;
  74. 71 }

转载于:https://www.cnblogs.com/1013star/p/11198204.html

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读