Advertisement
Josif_tepe

Untitled

Mar 10th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int absolute_value(int a) {
  6.     if(a < 0) {
  7.         return -a;
  8.     }
  9.     else {
  10.         return a;
  11.     }
  12. }
  13. int min(int a, int b) {
  14.     if(a < b) {
  15.         return a;
  16.     }
  17.     else {
  18.         return b;
  19.     }
  20. }
  21. int main()
  22. {
  23.     int a, b;
  24.     cin >> a >> b;
  25.     int cekori = 0;
  26.     while(a > 0 and b > 0) {
  27.         int cifra_a = a % 10;
  28.         int cifra_b = b % 10;
  29.         int prv_nacin = absolute_value(cifra_a - cifra_b);
  30.         int vtor_nacin = 10 - cifra_a + cifra_b;
  31.         int tret_nacin = 10 - cifra_b + cifra_a;
  32.         int pomal_od_prv_i_vtor = min(prv_nacin, vtor_nacin);
  33.         int najmal = min(pomal_od_prv_i_vtor, tret_nacin);
  34.         cekori += najmal;
  35.         a /= 10;
  36.         b /= 10;
  37.     }
  38.     cout << cekori << endl;
  39.     return 0;
  40. }
  41. // 5
  42. // 4 3 1 2 5
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement