Advertisement
Josif_tepe

Untitled

Feb 17th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int absolute_value(int x) {
  5.     if(x < 0) {
  6.         return -x; // ako x e negativen mnozi go so -1; x * -1 = -x
  7.     }
  8.     else {
  9.         return x; // ako e pozitiven ili 0 si ostanuva isti
  10.     }
  11. }
  12. int min(int a, int b) {
  13.     if(a < b) {
  14.         return a;
  15.     }
  16.     else {
  17.         return b;
  18.     }
  19. }
  20. int main()
  21. {
  22.     int p, k;
  23.     cin >> p >> k;
  24.     int rezultat = 0;
  25.     while(p > 0 and k > 0) {
  26.         int cifra_p = p % 10;
  27.         int cifra_k = k % 10;
  28.         int razlika1 = absolute_value(cifra_p - cifra_k);
  29.         int razlika2 = absolute_value(10 - cifra_p) + cifra_k;
  30.         int razlika3 = absolute_value(10 - cifra_k) + cifra_p;
  31.         rezultat += min(razlika1, min(razlika2, razlika3));
  32.         p /= 10;
  33.         k /= 10;
  34.     }
  35.     cout << rezultat << endl;
  36.  
  37.  
  38.  
  39.     return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement