Advertisement
allia

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

Dec 8th, 2020
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 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 proverka (int *a, int *b, int n)
  68. {
  69.   int znach = 0;
  70.  
  71.   for (int i =0; i < n; i++)
  72.   {
  73.     if (a[i] > b[i])
  74.       {
  75.         znach = 1;
  76.         break;
  77.       }
  78.     if (a[i] < b[i])
  79.      {
  80.         znach = -1;
  81.         break;
  82.       }
  83.   }
  84.  
  85.   return znach;
  86. }
  87. int main()
  88. {
  89.   int n, m, f, parametr = 0;
  90.  
  91.   cin >> n >> m;
  92.   int s = n-1, c = m-1;
  93.  
  94.   int *a = new int[n];
  95.   for ( int i = 0; i < n; i++)
  96.    cin >> a[i];
  97.  
  98.   int *b = new int[m];
  99.   for (int i = 0; i < m; i++)
  100.      cin >> b[i];
  101.  
  102. if (m == n)
  103.  parametr = proverka(a, b, n);
  104.  
  105. if (n > m || ( n == m && parametr == 1))
  106.  minuss(a, b, n, m, n);
  107. else if (m > n || (n == m && parametr == -1))
  108.  {
  109.    cout << "-";
  110.    minuss(b, a, m, n, m);
  111.  }
  112.  else if (m == n && parametr == 0)
  113.  cout << 0;
  114.  
  115. }
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement