HDOJ - 1002 - A + B Problem II

2014-11-24 10:10:39 · 作者: · 浏览: 2

题意:大数相加,但是AC率好低,注意进位和输出细节。

参考测试数据:

6
1 2
1 0
9999 1(找出错误的)
1 999999
5555 4445
112233445566778899 998877665544332211

代码:

#include 
  
       
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             using namespace std; const int maxn = 1000+10; int a[maxn], b[maxn], s[maxn]; char s1[maxn], s2[maxn]; void Trans() { int i = 0, j = 0; for(i = strlen(s1)-1, j = 0; i >=0; i--) a[j++] = s1[i] - '0'; for(i = strlen(s2)-1, j = 0; i >=0; i--) b[j++] = s2[i] - '0'; } void Sum() { int i = 0; for (i = 0; i < strlen(s1) || i < strlen(s2); i++) { int temp = a[i] + b[i] + s[i]; s[i] = temp % 10; s[i+1] = temp / 10; } } int main() { #ifdef Local freopen("a.in", "r", stdin); #endif int t = 0, i = 0; cin >> t; getchar(); for (int kase = 1; kase <= t; kase++) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(s, 0, sizeof(s)); cout << "Case " << kase << ":" << endl; cin >> s1 >> s2; Trans(); Sum(); cout << s1; cout << " + "; cout << s2; cout << " = "; for (i = maxn-1; s[i] == 0 && i >= 0; i--); for (; i >=0; i--) cout << s[i] ; cout << endl; if (kase != t) cout << endl; } }