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  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  and , where  () is the number of significant digits, and  and  are the two float numbers to be compared. Each float number is non-negative, no greater than , 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

 

17/25

复制代码
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include<math.h>
  5. #include<queue>
  6. #include<vector>
  7. #include<set>
  8. using namespace std;
  9.  
  10. int N;//精度
  11. string A;
  12. string B;
  13.  
  14. bool is_zero(string a) {//判断0
  15. if (a.length() == 1 && a[0] == 0) {
  16. return 1;
  17. }
  18. return 0;
  19. }
  20.  
  21. string to_form(int &index, string a) {//这里不考虑a=0
  22. //int index = 0;//记录指数
  23. int len = a.length();
  24. string str;//记录去0后的数
  25. if (a[0] == '0') {//处理0.0000000...这类数
  26. for (int i = 0; i < len; i++) {
  27. if (a[i] >'0'&&a[i]<'9') {
  28. index = -(i - 2);
  29. str = a.substr(i, len - i);
  30. break;
  31. }
  32. }
  33. }
  34. else {//处理123456.789......和12345600......
  35. for (int i = 1; i < len; i++) {
  36. if (a[i] == '.') {
  37. index = i;
  38. str = a.substr(0, 5) + a.substr(i + 1, len - i + 1);
  39. break;
  40. }
  41. else if(i==len-1){//最后一位肯定不是小数点,所有只有整数才能遍历到最后一位
  42. index = len;
  43. str = a;
  44. }
  45. }
  46. }
  47. return "0." + str;
  48. }
  49.  
  50. string deal(string a, int n,int &index) {//输出格式化
  51. string str;
  52. if (is_zero(a) == true) {
  53. for (int i = 0; i < n; i++) {
  54. str += '0';
  55. }
  56. str = "0." + str;
  57. return str;
  58. }
  59. else {
  60. str = to_form(index, a);
  61. if (str.length() < (n + 2)) {//位数不足后面补零
  62. for (int i = 0; i < (n + 2 - str.length()); i++) {
  63. str = str + '0';
  64. }
  65. }
  66. if (str.length() > (n + 2)) {//位数过长截尾
  67. str.erase(n + 2, str.length() - (n + 2));
  68. }
  69. return str;
  70. }
  71. }
  72.  
  73. int main() {
  74. cin >> N;
  75. cin >> A;
  76. cin >> B;
  77. int index_a = 0;
  78. int index_b = 0;//a,b指数
  79. string s_a;
  80. string s_b;
  81. s_a = deal(A,N,index_a);
  82. s_b = deal(B, N,index_b);
  83. //cout << s_a << "*10^" << index_a;
  84. if (s_a == s_b&&index_a==index_b) {
  85. cout << "YES " << s_a<<"*10^"<<index_a;
  86. }
  87. else {
  88. cout << "NO " << s_a <<"*10^"<<index_a<< ' ' << s_b<<"*10^"<<index_b;
  89. }
  90. }