Advertisement
allia

длинная арифметика сложение

Sep 23rd, 2020
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.   int n=0, m=0, c=0, f=0;
  8.   cin >> n >> m;
  9.   int *arr, *ar;
  10.  
  11.   arr = new int[n];
  12.   ar = new int[m];
  13.  
  14.   for (int i=0; i<n; i++)
  15.   cin >> arr[i];
  16.  
  17.   for (int j=0; j<m; j++)
  18.   cin >> ar[j];
  19.  
  20.   if (n>=m)
  21.   f=n+1;
  22.   else f=m+1;
  23.  
  24.   int res[f];
  25.  
  26.   int k=n-1;
  27.   int j=m-1;
  28.   int l=f-1;
  29.  
  30.   for (int x=0; x<f; x++)
  31.        res[x]=0;
  32.  
  33.   while (k>=0 && j>=0)
  34.   {
  35.     c = arr[k]+ar[j]+res[l];
  36.     if (c>=10)
  37.     {
  38.       res[l]=c%10;
  39.       res[l-1]+=c/10;
  40.     }
  41.     else
  42.       res[l]=c;
  43.       k--;
  44.       j--;
  45.       l--;
  46.   }
  47.  
  48.     while (k>=0)
  49.   {
  50.      c = arr[k]+res[l];
  51.     if (c>=10)
  52.     {
  53.       res[l]=c%10;
  54.       res[l-1]+=c/10;
  55.     }
  56.     else
  57.       res[l]=c;
  58.       k--;
  59.       l--;
  60.   }
  61.  
  62.   while (j>=0)
  63.   {
  64.      c = ar[j]+res[l];
  65.     if (c>=10)
  66.     {
  67.       res[l]=c%10;
  68.       res[l-1]+=c/10;
  69.     }
  70.     else
  71.       res[l]=c;
  72.       j--;
  73.       l--;
  74.   }
  75.  
  76. if (res[0]!=0)
  77.   for (int i=0; i<f; i++)
  78.    cout << res[i];
  79. else
  80. for (int i=1; i<f; i++)
  81.    cout << res[i];
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement