Advertisement
adiboier

Scadere nr mari

Feb 6th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4. ifstream fin("date.in");
  5. ofstream fout("date.out");
  6. int a[1000],b[1000];
  7. int main()
  8. {
  9.     char c;
  10.     while(fin>>c)
  11.     {
  12.         if(c>='0' && c<='9')
  13.         {
  14.             a[0]++;
  15.             a[a[0]]=c-48;
  16.         }
  17.         else break;
  18.     }
  19.     int k;
  20.     fin>>k;
  21.     while(k)
  22.     {
  23.         b[0]++;
  24.         b[b[0]]=k%10;
  25.         k/=10;
  26.     }
  27.     for(int i=1;i<=a[0]/2;i++)
  28.     {
  29.         int aux=a[i];
  30.         a[i]=a[a[0]-i+1];
  31.         a[a[0]-i+1]=aux;
  32.     }
  33.     int transport=0;
  34.     if(a[0]>b[0])
  35.     {
  36.         for(int i=1;i<=a[0];i++)
  37.         {
  38.             if(transport==1)
  39.             {
  40.                  a[i]--;
  41.                  transport=0;
  42.             }
  43.             if(a[i]-b[i]<0)
  44.             {
  45.                 transport=1;
  46.                 a[i]=a[i]+10-b[i];
  47.             }
  48.             else a[i]-=b[i];
  49.         }
  50.         for(int i=a[0];i>=1;i--)
  51.             fout<<a[i];
  52.     }
  53.     else if(b[0]>a[0])
  54.     {
  55.         for(int i=1;i<=b[0];i++)
  56.         {
  57.             if(transport==1)
  58.             {
  59.                  b[i]--;
  60.                  transport=0;
  61.             }
  62.             if(b[i]-a[i]<0)
  63.             {
  64.                 transport=1;
  65.                 b[i]=b[i]+10-a[i];
  66.             }
  67.             else b[i]-=a[i];
  68.         }
  69.         for(int i=b[0];i>=1;i--)
  70.             fout<<b[i];
  71.     }
  72.     else if(b[0]==a[0])
  73.     {
  74.         int oka=1,okb=1,okgen=1;
  75.         for(int i=1;i<=b[0] && okgen==1;i++)
  76.         {
  77.             if(b[i]>a[i])
  78.             {
  79.                  okb=0;
  80.                  okgen=0;
  81.             }
  82.             else
  83.             {
  84.                 oka=0;
  85.                 okgen=0;
  86.             }
  87.         }
  88.         if(oka==0)
  89.         {
  90.             for(int i=1;i<=a[0];i++)
  91.             {
  92.                 if(transport==1)
  93.                 {
  94.                      a[i]--;
  95.                      transport=0;
  96.                 }
  97.                 if(a[i]-b[i]<0)
  98.                 {
  99.                     transport=1;
  100.                     a[i]=a[i]+10-b[i];
  101.                 }
  102.                 else a[i]-=b[i];
  103.             }
  104.             for(int i=a[0];i>=1;i--)
  105.                 fout<<a[i];
  106.         }
  107.         else
  108.         {
  109.             for(int i=1;i<=b[0];i++)
  110.             {
  111.                 if(transport==1)
  112.                 {
  113.                      b[i]--;
  114.                      transport=0;
  115.                 }
  116.                 if(b[i]-a[i]<0)
  117.                 {
  118.                     transport=1;
  119.                     b[i]=b[i]+10-a[i];
  120.                 }
  121.                 else b[i]-=a[i];
  122.             }
  123.             for(int i=b[0];i>=1;i--)
  124.                 fout<<b[i];
  125.         }
  126.     }
  127.     fin.close();
  128.     fout.close();
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement