Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: C++  |  size: 1.47 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. using namespace std;
  5. string s1,s2;
  6. int a1[1002],a2[1002],res[1002];
  7. char s3[1002];
  8.  
  9. int main ()
  10. {
  11.         int l1,l2,k,i;
  12.     //freopen ("input.txt","r",stdin);
  13.     //freopen ("output.txt","w",stdout);
  14.         cin>>s1;
  15.         cin>>s2;
  16.         l1=s1.length();
  17.         l2=s2.length();
  18.         for (i=0; i<l1; i++)
  19.                 a1[i]=(s1[l1-i-1]-'0');
  20.         for (i=0; i<l2; i++)
  21.                 a2[i]=(s2[l2-i-1]-'0');
  22.         if (l1>l2)
  23.         {
  24.                 for (i=0; i<l1-1; i++)
  25.                 {
  26.                         if (a1[i]>=a2[i])
  27.                                 res[i]=a1[i]-a2[i];
  28.                         else
  29.                         {
  30.                                 res[i]=10+a1[i]-a2[i];
  31.                                 a1[i+1]--;
  32.                         }
  33.                 }
  34.                 res[l1-1]=a1[l1-1];
  35.         }
  36.         else {
  37.         if (l2>l1)
  38.         {
  39.                 //printf ("-");
  40.                 cout << "-";
  41.                 for (i=0; i<l2-1; i++)
  42.                 {
  43.                         if (a2[i]>=a1[i])
  44.                                 res[i]=a2[i]-a1[i];
  45.                         else
  46.                         {
  47.                                 res[i]=10+a2[i]-a1[i];
  48.                                 a2[i+1]--;
  49.                         }
  50.                 }
  51.                 res[l2-1]=a2[l2-1];
  52.         }
  53.         else {
  54.         if (l1==l2)
  55.         {
  56.                 if (a1[l1-1]>=a2[l1-1])
  57.                 {
  58.                         for (i=0; i<l1-1; i++)
  59.                         {
  60.                                 if (a1[i]>=a2[i])
  61.                                         res[i]=a1[i]-a2[i];
  62.                                 else
  63.                                 {
  64.                                         res[i]=10+a1[i]-a2[i];
  65.                                         a1[i+1]--;
  66.                                 }
  67.                         }
  68.                         res[l1-1]=a1[l1-1]-a2[l1-1];
  69.                 }
  70.        
  71.         else
  72.         {
  73.                 //printf ("-");
  74.                 cout << "-";
  75.                 for (i=0; i<l2-1; i++)
  76.                 {
  77.                         if (a2[i]>=a1[i])
  78.                                 res[i]=a2[i]-a1[i];
  79.                         else
  80.                         {
  81.                                 res[i]=10+a2[i]-a1[i];
  82.                                 a2[i+1]--;
  83.                         }
  84.                 }
  85.                 res[l2-1]=a2[l2-1]-a1[l2-1];
  86.         }
  87. }
  88.         }}
  89.         k=1002;
  90.         while ((res[k]==0)&&(k!=-1))
  91.                 k--;
  92.         if (k==-1)
  93.                 printf("0");
  94.         else
  95.         {
  96.                 for (i=k; i>=0; i--)
  97.                         cout<<res[i];
  98.         }
  99.         cout<<endl;
  100.         return 0;
  101. }