[hackerrank]Closest Number

https://www.hackerrank.com/contests/w5/challenges/closest-number

简单题。

#include <iostream>

#include <cmath>

using namespace std;



int main() {

    int T;

    cin >> T;

    while (T--) {

        int a, b, x;

        cin >> a >> b >> x;

        if (b < 0 && a == 1) {

            if (x - 1 < 1) {

                cout << x << endl;

            } else {

                cout << 0 << endl;

            }

        } else if (b < 0) {

            cout << 0 << endl;

        } else {

            int req = pow(a, b);

            int k = req / x;

            if ((k + 1) * x - req < req - k * x) {

                cout << (k + 1) * x << endl;

            } else {

                cout << k * x << endl;

            }

        }

    }

    return 0;

}

  

你可能感兴趣的:(number)