1. 程式人生 > >杭電OJ1002

杭電OJ1002

#include
#include
using namespace std;
int main()
{
 string A, B;
 int a[9999], b[9999], sum[9999], x, len1, len2,l=1;
 cin >> x;
 while (x)
 {
  cin >> A >> B;
  len1 = A.size();
  len2 = B.size();
  int i, j, m, n, t = 0, s;
  for (i = 0; A[i] != ‘\0’; i++)
   a[i] = A[i] - 48;
  for (j = 0; B[j] != ‘\0’; j++)
    b[j] = B[j] - 48;
   if (len1 >= len2)
   {
    s = 0;
    for (m = len1 - 1, n = len2 - 1; m >= 0 && n >= 0; m–, n–)
    {
     sum[s++] = (a[m] + b[n] + t) % 10;
     t = (a[m] + b[n] + t) / 10;
    }
    for (; m >= 0; m–)
    {
     sum[s++] = (a[m] + t) % 10;
     t = (a[m] + t) / 10;
    }
   }
   else
   {
    s = 0;
    for (m = len1 - 1, n = len2 - 1; m >= 0 && n >= 0; m–, n–)
    {
     sum[s++] = (a[m] + b[n] + t) % 10;
     t = (a[m] + b[n] + t) / 10;
    }
    for (; n >= 0; n–)
    {
     sum[s++] = (b[n] + t) % 10;
     t = (b[n] + t) / 10;
    }
   }
   cout << “Case”<<’ '<< l++ << ‘:’ << endl;
   cout << A << ’ ’ << ‘+’ << ’ ‘<<B<<’ '<< ‘=’ << ’ ';
   for (int z=s-1; z >= 0; z–)
    cout << sum[z];
   cout << endl;
   if (x>1)
   cout << endl;
   x–;
  }
 }