Wednesday, June 18, 2014

Hackerrank Weekly challenges - Week 5 - Closest Number solution

Hackerrank Weekly challenges - Week 5 - Closest Number : https://www.hackerrank.com/contests/w5/challenges/closest-number

Hackerrank Weekly challenges - Week 5 - Closest Number editorial: not published yet (at the time of posting)

Hackerrank Weekly challenges - Week 5 - Closest Number solution: 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int main() {
    int t, a, b, x, r, l, m;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &a, &b, &x);
        a=pow(a, b);
        m=a/x;
        l=x*m, r=x*(m+1);
        m=min(a-l, r-a);
        if(l+m==a) printf("%d\n", l);
        else printf("%d\n", r);
    }
    return 0;
}

note: if input is
1
5 1 3
then output should be 6 not 3.

No comments:

Post a Comment