码迷,mamicode.com
首页 > 其他好文 > 详细

1060. Are They Equal (25)

时间:2015-12-06 12:57:30      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:


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

 

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





1060. Are They Equal (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!