Advertisement
alfian23

prob-[A] Seberapa Jauhkah

Sep 12th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Koodinat
  5. {
  6. public:
  7.   int x;
  8.   int y;
  9.  
  10.   Koodinat(int x1, int y1)
  11.   {
  12.     x = x1;
  13.     y = y1;
  14.   }
  15.  
  16.   void increaseX()
  17.   {
  18.     x++;
  19.     return;
  20.   }
  21.  
  22.   void increaseY()
  23.   {
  24.     y++;
  25.     return;
  26.   }
  27. };
  28.  
  29. int main()
  30. {
  31.   int xAwal, yAwal, xTujuan, yTujuan;
  32.   cin >> xAwal >> yAwal;
  33.   cin >> xTujuan >> yTujuan;
  34.  
  35.   Koodinat lokasiAwal(xAwal, yAwal);
  36.   Koodinat lokasiTujuan(xTujuan, yTujuan);
  37.   Koodinat lokasiStep(lokasiAwal.x, lokasiAwal.y);
  38.  
  39.   int result = 0;
  40.   bool isRun = true;
  41.  
  42.   while (isRun)
  43.   {
  44.     if (lokasiStep.x != lokasiTujuan.x)
  45.     {
  46.       lokasiStep.increaseX();
  47.       result++;
  48.     }
  49.  
  50.     if (lokasiStep.y != lokasiTujuan.y)
  51.     {
  52.       lokasiStep.increaseY();
  53.       result++;
  54.     }
  55.  
  56.     if (lokasiStep.x == lokasiTujuan.x && lokasiStep.y == lokasiTujuan.y)
  57.       isRun = false;
  58.   }
  59.  
  60.   cout << result;
  61. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement