Advertisement
allia

длинная арифметика вычитание

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