1060. Are They Equal (25)

红太狼 2022-05-29 23:00 238阅读 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:

  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

题目大意:

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. char A[1000],B[1000],a[1000],b[1000];
  4. int main()
  5. {
  6. int i,j,n,cnta,cntb,pa,pb,indexa,indexb;
  7. scanf("%d %s %s",&n,a,b);
  8. cnta=strlen(a);
  9. cntb=strlen(b);
  10. for(i=0;i<strlen(a);i++)
  11. {
  12. if(a[i]=='.')
  13. {
  14. cnta=i;
  15. break;
  16. }
  17. }
  18. for(i=0;i<strlen(b);i++)
  19. {
  20. if(b[i]=='.')
  21. {
  22. cntb=i;
  23. break;
  24. }
  25. }
  26. pa=0;
  27. while(a[pa]=='0'||a[pa]=='.')
  28. pa++;
  29. pb=0;
  30. while(b[pb]=='0'||b[pb]=='.')
  31. pb++;
  32. if(cnta>=pa)
  33. {
  34. cnta=cnta-pa;
  35. }
  36. else
  37. {
  38. cnta=cnta-pa+1;
  39. }
  40. if(cntb>=pb)
  41. {
  42. cntb=cntb-pb;
  43. }
  44. else
  45. {
  46. cntb=cntb-pb+1;
  47. }
  48. if(pa==strlen(a))
  49. cnta=0;
  50. if(pb==strlen(b))
  51. cntb=0;
  52. indexa=0;
  53. while(indexa<n)
  54. {
  55. if(a[pa]!='.'&&pa<strlen(a))
  56. {
  57. A[indexa++]=a[pa];
  58. }
  59. else if(pa>=strlen(a))
  60. {
  61. A[indexa++]='0';
  62. }
  63. pa++;
  64. }
  65. A[indexa]='\0';
  66. indexb=0;
  67. while(indexb<n)
  68. {
  69. if(b[pb]!='.'&&pb<strlen(b))
  70. {
  71. B[indexb++]=b[pb];
  72. }
  73. else if(pb>=strlen(b))
  74. {
  75. B[indexb++]='0';
  76. }
  77. pb++;
  78. }
  79. B[indexb]='\0';
  80. if(strcmp(A,B)==0&&cnta==cntb)
  81. {
  82. printf("YES 0.%s*10^%d\n",A,cnta);
  83. }
  84. else
  85. {
  86. printf("NO 0.%s*10^%d 0.%s*10^%d\n",A,cnta,B,cntb);
  87. }
  88. return 0;
  89. }

参考: 柳婼

发表评论

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

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

相关阅读

    相关 1060 爱丁顿数 (25 分)

    题目:[1060 爱丁顿数 (25 分)][1060_25] > 英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数” E ,即满足有 E

    相关 1060. 爱丁顿数(25)

    英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数”E,即满足有E天骑车超过E英里的最大整数E。据说爱丁顿自己的E等于87。 现给定某人N天的