• 1060 Are They Equal


    1060 Are They Equal

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

    总结:没有全部写出来,只有19分

    代码:

    ①:最后一个测试点是如果读取的位数

    ②:如果a=123.45 b=12 这个代码也是会打印出 YES(判断是否相等出错)

    1. #include
    2. using namespace std;
    3. int n;
    4. void count(string a){
    5. if(a=="0"){
    6. printf(" 0.0*10^0");
    7. return;
    8. }
    9. bool st=false;//表示是否遇到了数字
    10. string s;
    11. int cot=0,num=0;//cot表示指数,num表示数字的有效位数
    12. if(a[0]=='0'){
    13. for(int i=2;isize() && num
    14. if(a[i]=='0' && st==false) cot++;
    15. else{
    16. st=true;
    17. s+=a[i];
    18. num++;
    19. }
    20. }
    21. }
    22. else{
    23. for(int i=0;isize();i++){
    24. if(a[i]=='.'){
    25. st=true;
    26. continue;
    27. }
    28. if(!st) cot++;
    29. if(num
    30. num++;
    31. s+=a[i];
    32. }
    33. }
    34. }
    35. printf(" 0.");
    36. cout << s;
    37. printf("*10^%d",cot);
    38. return;
    39. }
    40. int main(){
    41. string a,b;
    42. cin >> n >> a >> b;
    43. bool flag=false;
    44. for(int i=0;isize() && isize() && i
    45. if(a[i]!=b[i]) flag=true;
    46. if(!flag){
    47. printf("YES");
    48. count(a);
    49. }
    50. else{
    51. printf("NO");
    52. count(a);count(b);
    53. }
    54. return 0;
    55. }

     大佬代码:
     

    1. #include
    2. #include
    3. using namespace std;
    4. int main() {
    5. int n, p = 0, q = 0;
    6. char a[10000], b[10000], A[10000], B[10000];
    7. scanf("%d%s%s", &n, a, b);
    8. int cnta = strlen(a), cntb = strlen(b);
    9. //记录小数点的位置
    10. for(int i = 0; i < strlen(a); i++) {
    11. if(a[i] == '.') {
    12. cnta = i;
    13. break;
    14. }
    15. }
    16. for(int i = 0; i < strlen(b); i++) {
    17. if(b[i] == '.') {
    18. cntb = i;
    19. break;
    20. }
    21. }
    22. int indexa = 0, indexb = 0;
    23. //找到第一个有效数字(即非0且不是小数点的位置)
    24. while(a[p] == '0' || a[p] == '.') p++;
    25. while(b[q] == '0' || b[q] == '.') q++;
    26. //找到次方的大小
    27. if(cnta >= p)
    28. cnta = cnta - p;
    29. else
    30. cnta = cnta - p + 1;
    31. if(cntb >= q)
    32. cntb = cntb - q;
    33. else
    34. cntb = cntb - q + 1;
    35. if(p == strlen(a))
    36. cnta = 0;
    37. if(q == strlen(b))
    38. cntb = 0;
    39. //从有效数字位开始读取数字
    40. while(indexa < n) {
    41. //如果没有超出数的范围且不是小数点
    42. if(a[p] != '.' && p < strlen(a))
    43. A[indexa++] = a[p];
    44. //如果超过了数的范围则补0
    45. else if(p >= strlen(a))
    46. A[indexa++] = '0';
    47. p++;
    48. }
    49. while(indexb < n) {
    50. if(b[q] != '.' && q < strlen(b))
    51. B[indexb++] = b[q];
    52. else if(q >= strlen(b))
    53. B[indexb++] = '0';
    54. q++;
    55. }
    56. if(strcmp(A, B) == 0 && cnta == cntb)
    57. printf("YES 0.%s*10^%d", A, cnta);
    58. else
    59. printf("NO 0.%s*10^%d 0.%s*10^%d" , A, cnta, B, cntb);
    60. return 0;
    61. }

    好好学习,天天向上!

    我要考研

  • 相关阅读:
    第一个MyBatis程序
    -钞票兑换-
    JS控制显示或隐藏TR
    这几个小妙招让你学会如何压缩图片大小
    【MySQL】性能分析工具EXPLAIN
    安卓LeakCanary研究
    网络信息安全软考笔记(1)
    K8S用户管理体系介绍
    【数据结构与算法】二叉树——堆
    如何当好硬软件助理工程师——实习周报(一)
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/127111813