UVA10673 - Play with Floor and Ceil(数论)

UVA10673 - Play with Floor and Ceil(数论)

题目链接

题目大意:给你x,k,要求你找出p,q:满足x = p下取整(x/k) + q上取整(x/k);

解题思路:分三种情况:1、x整除k,那么可以另p = 0,那么q = k。
2、x不整除k,那么另n=下取整(x/k),则x=pn + q(n + 1)= (p + q)*n + q;那么就可以让q = x%k。那么(p + q)= x/(x/k)。p = x/(x/k) - q;
3、x<k的情况。要单独处理不然会除到0.

代码:

#include <cstdio>
#include <cstring>

int main () {

    int T, X, K;
    scanf ("%d", &T);
    while (T--)  {

        scanf ("%d%d", &X, &K);
        if (X < K) {
            printf ("%d %d\n", 0, X);
            continue;
        }

        if (X % K == 0) 
            printf ("0 %d\n", K);
        else {
            int q = X % (X / K);
            int p = X / (X / K) - q;
            printf ("%d %d\n", p, q);
        }
    }
    return 0;
}

你可能感兴趣的:(UVA10673 - Play with Floor and Ceil(数论))