1060. Are They Equal (25)

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

来源: <http://www.patest.cn/contests/pat-a-practise/1060>

  1. #include<string>
  2. #include <iostream>
  3. using namespace std;
  4. int n;
  5. int SearchDot(string s) { //Search the pos of ‘.‘
  6. for (int i = 0; i < s.length(); i++) {
  7. if (s[i] == ‘.‘)
  8. return i;
  9. }
  10. return -1;
  11. }
  12. string ChangeToSci(string s1) {
  13. string s1new;
  14. bool isZero = true;//check whether it is 0
  15. for (int i = 0; i < s1.length(); i++) {
  16. if (s1[i] != ‘0‘&&s1[i] != ‘.‘)
  17. isZero = false;
  18. }
  19. if (isZero) {
  20. s1new += "0.";
  21. for (int i = 0; i < n; i++)
  22. s1new += ‘0‘;
  23. s1new += "*10^0";
  24. return s1new;//if 0 return
  25. }
  26. bool hengji = false;
  27. string temp;
  28. for (int i = 0; i < s1.length()-1; i++) {//remove 0 in the head such as 05.02
  29. if (s1[i+1]>=‘0‘&&s1[i+1]<=‘9‘&&s1[i] == ‘0‘&&hengji == false) {
  30. continue;
  31. }
  32. else {
  33. hengji = true;
  34. }
  35. temp += s1[i];
  36. }
  37. temp += s1[s1.length() - 1];
  38. s1 = temp;
  39. int dotPos = SearchDot(s1);
  40. if (dotPos == -1) {//no dot in the number ,such like 12345
  41. s1new += "0.";
  42. for (int i = 0; i < n; i++) {
  43. if (i < s1.length())
  44. s1new += s1[i];
  45. else
  46. s1new += ‘0‘;
  47. }
  48. s1new += "*10^";
  49. int temp = s1.length();
  50. if (temp == 100)
  51. s1new += "100";
  52. else if (temp >= 10) {
  53. s1new += temp / 10 + ‘0‘;
  54. s1new += temp % 10 + ‘0‘;
  55. }
  56. else
  57. s1new += temp + ‘0‘;
  58. }
  59. else if (dotPos != 1) {//dot not at the second pos, such like 123.45 12.3
  60. s1new += "0.";
  61. bool dotFlag = false;
  62. for (int i = 0; i < n; i++) {
  63. if (i < s1.length()) {
  64. if (s1[i] == ‘.‘) {
  65. dotFlag = true;
  66. continue;
  67. }
  68. s1new += s1[i];
  69. }
  70. else
  71. s1new += ‘0‘;
  72. }
  73. if (dotFlag == true) {
  74. if (n < s1.length())
  75. s1new += s1[n];
  76. else
  77. s1new += ‘0‘;
  78. }
  79. s1new += "*10^";
  80. int temp = dotPos;
  81. if (temp == 100)
  82. s1new += "100";
  83. else if (temp >= 10) {
  84. s1new += temp / 10 + ‘0‘;
  85. s1new += temp % 10 + ‘0‘;
  86. }
  87. else
  88. s1new += temp + ‘0‘;
  89. }
  90. else {
  91. if (s1[0] == ‘0‘) {//0.23 0.00023 and so on
  92. s1new += "0.";
  93. int j = 2, cnt = 0, flag = 0;
  94. while (true)
  95. {
  96. if (s1[j] == ‘0‘ && flag == 0)
  97. cnt++;
  98. else
  99. break;
  100. j++;
  101. }
  102. for (int i = j; i < j + n; i++) {
  103. if (i < s1.length())
  104. s1new += s1[i];
  105. else
  106. s1new += ‘0‘;
  107. }
  108. if (cnt == 0) {
  109. s1new += "*10^0";
  110. }
  111. else {
  112. s1new += "*10^-";
  113. int temp = cnt;
  114. if (temp == 100)
  115. s1new += "100";
  116. else if (temp >= 10) {
  117. s1new += temp / 10 + ‘0‘;
  118. s1new += temp % 10 + ‘0‘;
  119. }
  120. else
  121. s1new += temp + ‘0‘;
  122. }
  123. }
  124. else {//2.002 1.354 and so on
  125. s1new += "0.";
  126. for (int i = 0; i <= n; i++) {
  127. if (i == 1)
  128. continue;
  129. if (i < s1.length())
  130. s1new += s1[i];
  131. else
  132. s1new += ‘0‘;
  133. }
  134. s1new += "*10^1";
  135. }
  136. }
  137. return s1new;
  138. }
  139. int main(void) {
  140. string s1, s2;
  141. string s1new, s2new;
  142. cin >> n >> s1 >> s2;
  143. if (n == 0) {
  144. cout << "YES 0";
  145. return 0;
  146. }
  147. string s11, s22;
  148. s11 = ChangeToSci(s1);
  149. s22 = ChangeToSci(s2);
  150. if (s11 == s22) {
  151. cout << "YES " << s11;
  152. }
  153. else {
  154. cout << "NO " << s11 << " " << s22;
  155. }
  156. return 0;
  157. }

来自为知笔记(Wiz)

时间: 2024-12-17 21:05:16

1060. Are They Equal (25)的相关文章

PAT 1060. Are They Equal (25)

1060. Are They Equal (25) 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 a

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

1060 Are They Equal (25 分) 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

PAT Advanced 1060 Are They Equal (25分)

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 supp

PAT:1060. Are They Equal (25) AC

#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int n; string deal(string s,int &e) //[思维]1:吸收多余0:.2:找到“.”判断与1的大小.3:去除“.”的同时统计10的指数(正负与step2有关) { //4:判断是否删完了,删完了表明数字是0.5:存入前n个数字,不足用

PAT (Advanced Level) 1060. Are They Equal (25)

模拟题.坑点较多. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace

1060 Are They Equal (25 分)

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×10?5?? with simple chopping. Now given the number of significant digits on a machine and two float numbers, y

A1060. Are They Equal (25)

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

pat 1060 爱丁顿数(25 分)

英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个"爱丁顿数" E ,即满足有 E 天骑车超过 E 英里的最大整数 E.据说爱丁顿自己的 E 等于87. 现给定某人 N 天的骑车距离,请你算出对应的爱丁顿数 E(≤N). 输入格式: 输入第一行给出一个正整数 N (≤10?5??),即连续骑车的天数:第二行给出 N 个非负整数,代表每天的骑车距离. 输出格式: 在一行中给出 N 天的爱丁顿数. 输入样例: 10 6 7 6 9 3 10 8 2 7 8 输出样例:

PAT (Advanced Level) 1060 Are They Equal

题解 找出有效的字符串(t),第一个非零数字的位置(zero)和小数点的位置(point).   若 s = "0012.104654",N = 4 则 t = "12104654",zero = 2,point = 4,所以答案为 0.1210*10^(point-zero)   若 s = "0000.00010465",N = 4 则 t = "10465",zero = 8,point = 4,所以答案为 0.1046