zoukankan      html  css  js  c++  java
  • HDOJ(1002) A + B Problem II

    我自己写了一个版本,用于模拟小学数学中的加法操作过程,代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 const int MAX_LEN = 1002;
     5 
     6 void long_add(char *result, char *A, char *B)
     7 {
     8     int len_a = strlen(A), len_b = strlen(B);
     9     int a, b, t;
    10     int carry = 0;
    11 
    12     while (len_a > 0 || len_b > 0){
    13         if (len_a > 0) a = A[len_a-1] - '0';
    14         else a = 0;
    15 
    16         if (len_b > 0) b = B[len_b-1] - '0';
    17         else b = 0;
    18 
    19         t = a + b + carry;
    20         carry = t / 10; 
    21         t = t % 10;     
    22 
    23         if (len_a > len_b){
    24             result[len_a-1] = t + '0';
    25         }else{
    26             result[len_b-1] = t + '0';
    27         }
    28 
    29         len_a--;
    30         len_b--;
    31     }
    32 }
    33 
    34 void reset(char * array)
    35 {
    36     int i, len = sizeof(array);
    37     for (i = 0; i < len; i++)
    38         array[i] = '';
    39 }
    40 
    41 int main(void)
    42 {
    43     char A[MAX_LEN], B[MAX_LEN], RE[MAX_LEN]={''};
    44     int  i = 1, round;
    45 
    46     scanf("%d", &round);
    47     if (round < 1 || round > 20) return 1;
    48 
    49     while (i <= round)
    50     {
    51         if (EOF != scanf("%s %s", A, B)){
    52             reset(RE);
    53             long_add(RE, A, B);
    54             printf("Case %d:
    ", i++);
    55             printf("%s + %s = %s
    ", A, B, RE);
    56 
    57             if (i <= round) printf("
    ");
    58         }    
    59     }
    60 
    61     return 0;
    62 }

    然后,自己编辑了一个测试用的数据文件:

    再运行程序,运行结果如下图所示:

    感觉应该没问题,但是提交后的结果是Wrong,至今不知道为什么。

    下面的代码是网上有人提供的,可以通过(参考链接):

     1 #include <iostream>
     2 #include <string.h>
     3 using namespace std;
     4 void add ( char a[], char b[] )
     5 {
     6     char sum[1010] = {' '};
     7     int flg = 0;
     8     int temp = 0;
     9     int len_a = strlen ( a );
    10     int len_b = strlen ( b );
    11     int i = len_a;
    12     int j = len_b;
    13     for ( ; i > 0; i-- )
    14     {
    15         if ( j > 0 )
    16         {
    17             temp = a[i-1] + b[j-1] + flg - 96;
    18             j--;
    19         }
    20         else temp = a[i-1] + flg - 48;
    21         if ( temp >= 10 )
    22         {
    23             flg = 1;
    24         }
    25         else flg = 0;
    26         temp = temp % 10;
    27         sum[i] = temp + 48;
    28     }
    29     if ( flg == 1 ) sum[0] = 49;
    30     i = 0;
    31     while ( i <= len_a )
    32     {
    33         if ( sum[i] != ' ' ) cout << sum[i];
    34         i++;
    35     }
    36     cout << endl;
    37 }
    38 int main()
    39 {
    40     int N;
    41     while ( cin >> N )
    42     {
    43         for ( int i = 1; i <= N; i++ )
    44         {
    45             char a[1000];
    46             char b[1000];
    47             cin >> a;
    48             cin >> b;
    49             int len_a = strlen ( a );
    50             int len_b = strlen ( b );
    51             cout << "Case " << i << ":
    " << a << " + " << b << " = ";
    52             if ( len_a >= len_b )
    53             {
    54                 add ( a, b );
    55             }
    56             else add ( b, a );
    57             if ( i != N ) cout << endl;
    58         }
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    【CF580D】Kefa and Dishes
    【poj3311】Hie with the Pie
    校外实习-7.7
    校外实习-7.6
    校外实习-7.5
    校外实习-7.4
    作业九-课程总结(补充)
    作业九-课程总结
    作业四——结对编程四则运算
    作业三
  • 原文地址:https://www.cnblogs.com/xiaomanon/p/4460021.html
Copyright © 2011-2022 走看看