Advertisement
fahimkamal63

Closest number

Apr 17th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. int main(){
  6.     int t; cin >> t;
  7.     while(t--){
  8.         int n, m; cin >> n >> m;
  9.         int big = n, small = n;
  10.         while(big % m != 0) big++;
  11.         while(small % m != 0) small--;
  12.  
  13.         if(n < 0) swap(big, small);
  14.  
  15.         int big_diff = abs(big) - abs(n);
  16.         int small_diff = abs(n) - abs(small);
  17. /*
  18.         cout << big << ' ' << small << endl;
  19.         cout << big_diff << ' ' << small_diff << endl;
  20. */
  21.  
  22.         if(big_diff < small_diff) cout << big << endl;
  23.         else if(small_diff < big_diff) cout << small<< endl;
  24.         else if(big_diff == small_diff) {
  25.             int k = max(abs(big), abs(small));
  26.             if(k == abs(big)){
  27.                 cout << big << endl;
  28.             }
  29.             else cout << small << endl;
  30.         }
  31.  
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement