egogoboy

Годовой баланс

Jun 22nd, 2022 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>;
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. void preob(vector<int>& m, string& s, int x) {
  8.  
  9.     for (int i = 0 + x; i < s.size(); i++) {
  10.         m.push_back(s[i] - '0');
  11.     }
  12.  
  13. }
  14.  
  15. bool comp(int& x1, int& x2) {
  16.     return x1 > x2;
  17. }
  18.  
  19. void reverse_preob(int& x, vector<int>& s) {
  20.  
  21.     int nul = 0;
  22.     int temp = 1;
  23.  
  24.     for (int i = s.size() - 1; i >= 0; i--) {
  25.         x += s[i] * temp;
  26.         temp *= 10;
  27.     }
  28.  
  29. }
  30.  
  31. int main() {
  32.  
  33.     std::ifstream fin("input.txt");
  34.     std::ofstream fout("output.txt");
  35.  
  36.     string a, b;
  37.     fin >> a >> b;
  38.  
  39.     vector<int> temp_a, temp_b;
  40.     bool f = false;
  41.     if (a[0] == '-') {
  42.         int min = a[1] - '0';
  43.         for (int i = 2; i < a.size(); i++) {
  44.             if (a[i] - '0' < min && a[i] != '0') {
  45.                 min = a[i] - '0';
  46.                 swap(a[i], a[1]);
  47.             }
  48.         }
  49.         temp_a.push_back(min);
  50.         preob(temp_a, a, 2);
  51.         sort(temp_a.begin() + 1, temp_a.end());
  52.         f = true;
  53.     }
  54.     else {
  55.         preob(temp_a, a, 0);
  56.         sort(temp_a.begin(), temp_a.end(), comp);
  57.     }
  58.     int kb = 0;
  59.     if (b[0] == '-') {
  60.         preob(temp_b, b, 1);
  61.         sort(temp_b.begin(), temp_b.end(), comp);
  62.         reverse_preob(kb, temp_b);
  63.         kb *= -1;
  64.     }
  65.     else{
  66.         int min = b[0] - '0';
  67.         for (int i = 1; i < b.size(); i++) {
  68.             if (b[i] - '0' < min && b[i] - '0' != 0) {
  69.                 min = b[i] - '0';
  70.                 swap(b[i], b[0]);
  71.             }
  72.         }
  73.         temp_b.push_back(min);
  74.         preob(temp_b, b, 1);
  75.         sort(temp_b.begin() + 1, temp_b.end());
  76.         reverse_preob(kb, temp_b);
  77.     }
  78.  
  79.     int ka = 0;
  80.     reverse_preob(ka, temp_a);
  81.     if (f) {
  82.         ka *= -1;
  83.     }
  84.  
  85.     fout << ka - kb << endl;
  86.     return 0;
  87.  
  88. }
Add Comment
Please, Sign In to add comment