Advertisement
TwITe

Untitled

Jan 30th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #ifndef TECH_BIG_NUMS_H
  2. #define TECH_BIG_NUMS_H
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. const int base = 10;
  8.  
  9. void multiplicate_big_nums(vector<int>& a, int b) { // умножение длинного на короткое
  10.     int carry = 0;
  11.     for (size_t i = 0; i < a.size() || carry; i++) {
  12.         if (i == a.size()) {
  13.             a.push_back(0);
  14.         }
  15.         long long cur = carry + a[i] * b;
  16.         a[i] = int(cur % base);
  17.         carry = int(cur / base);
  18.     }
  19.     while (a.size() > 1 && a.back() == 0) {
  20.         a.pop_back();
  21.     }
  22. }
  23.  
  24. void add_big_nums(vector<int>& a, const vector<int>& b) {
  25.     int carry = 0;
  26.     for (size_t i = 0; i < max(a.size(), b.size()) || carry; i++) {
  27.         if (i == a.size()) {
  28.             a.push_back(0);
  29.         }
  30.         a[i] += carry + (i < b.size() ? b[i] : 0);
  31.         carry = a[i] >= base;
  32.         if (carry) a[i] -= base;
  33.     }
  34. }
  35.  
  36. void substract_big_nums(vector<int>& a, vector<int>& b) {
  37.     int carry = 0;
  38.     for (size_t i = 0; i < b.size() || carry; i++) {
  39.         a[i] -= carry + (i < b.size() ? b[i] : 0);
  40.         carry = a[i] < 0;
  41.         if (carry) a[i] += base;
  42.     }
  43.     while (a.size() > 1 && a.back() == 0) {
  44.         a.pop_back();
  45.     }
  46. }
  47.  
  48. int compare_big_nums(const vector<int>& a, const vector<int>& b) {
  49.     if (a.size() > b.size()) return 1;
  50.     if (a.size() < b.size()) return -1;
  51.  
  52.     for (int i = (int) a.size() - 1; i >= 0; i--) {
  53.         if (a[i] - '0' > b[i] - '0') {
  54.             return 1;
  55.         }
  56.         if (a[i] - '0' < b[i] - '0') {
  57.             return -1;
  58.         }
  59.     }
  60.     return 0;
  61. }
  62.  
  63. bool compare_3big_nums(int& min_num, int& average_num, int& max_num, vector<int>& a, vector<int>& b, vector<int>& c) {
  64.     int res1 = compare_big_nums(a, b);
  65.     int res2 = compare_big_nums(a, c);
  66.     int res3 = compare_big_nums(b, c);
  67.  
  68.     if (res1 == 0 && res2 == 0 && res3 == 0) {
  69.         return false;
  70.     }
  71.  
  72.     //find max
  73.     if (res1 >= 0 && res2 >= 0) {
  74.         max_num = 1;
  75.     }
  76.     else if (res1 == -1 && res3 == 1) {
  77.         max_num = 2;
  78.     }
  79.     else {
  80.         max_num = 3;
  81.     }
  82.  
  83.     //find min
  84.     if (res1 == -1 && res2 == -1) {
  85.         min_num = 1;
  86.     }
  87.     else if (res1 >= 0 && res3 == -1) {
  88.         min_num = 2;
  89.     }
  90.     else {
  91.         min_num = 3;
  92.     }
  93.  
  94.     //find average
  95.     if ((min_num == 1 && max_num == 3) || (min_num == 3 && max_num == 1)) {
  96.         if (res1 != 0 && res3 != 0) {
  97.             average_num = 2;
  98.         }
  99.     }
  100.     if ((min_num == 2 && max_num == 3) || (min_num == 3 && max_num == 2)) {
  101.         if (res1 != 0 && res2 != 0) {
  102.             average_num = 1;
  103.         }
  104.     }
  105.     if ((min_num == 1 && max_num == 2) || (min_num == 2 && max_num == 1)) {
  106.         if (res2 != 0 && res3 != 0) {
  107.             average_num = 3;
  108.         }
  109.     }
  110.  
  111.     return true;
  112. }
  113.  
  114. void print_bug_num(const vector<int>& number) {
  115.     for (int i = number.size() - 1; i >= 0; i--) {
  116.         cout << number[i];
  117.     }
  118.     cout << endl;
  119. }
  120.  
  121. void save_big_num(vector<int>& a, const string& s) {
  122.     for (int i = (int)s.size() - 1; i >= 0; i--) {
  123.         a.push_back(s[i] - '0');
  124.     }
  125. }
  126.  
  127. #endif //TECH_BIG_NUMS_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement