[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;
}

  

posted @ 2014-08-13 18:24  阿牧遥  阅读(255)  评论(0编辑  收藏  举报