UVA10673 Play with Floor and Ceil【暴力枚举】

    Theorem
    For any two integers x and k there exists two more integers p and q such that:
在这里插入图片描述
    It’s a fairly easy task to prove this theorem, so we’d not ask you to do that. We’d ask for something even easier! Given the values of x and k, you’d only need to find integers p and q that satisfies the given equation.
Input
The first line of the input contains an integer, T (1 ≤ T ≤ 1000) that gives you the number of test cases. In each of the following T lines youd be given two positive integers x and k. You can safely assume that x and k will always be less than 10^8.
Output
For each of the test cases print two integers: p and q in one line. These two integers are to be separated by a single space. If there are multiple pairs of p and q that satisfy the equation, any one would do. But to help us keep our task simple, please make sure that the values,
在这里插入图片描述
fit in a 64 bit signed integer.
Sample Input
3
5 2
40 2
24444 6
Sample Output
1 1
1 1
0 6

问题链接UVA10673 Play with Floor and Ceil
问题简述:(略)
问题分析
    查找满足题目中公式的p和q,枚举p(1-k),找到p和q则输出p和q。
    另外一种方法是经过推导得出p和q的一般式子进行计算。参见代码。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* UVA10673 Play with Floor and Ceil */

#include <bits/stdc++.h>

using namespace std;

int main()
{

    int n, x, k;
    scanf("%d", &n);
    while (n--) {
        scanf("%d%d", &x, &k);
        printf("%d %d\n", k - x % k, x % k);
    }

    return 0;
}

AC的C++程序(枚举)如下:

/* UVA10673 Play with Floor and Ceil */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        LL x, k;
        scanf("%lld %lld", &x, &k);
        LL p, q, fl = floor((double) x / k), ce = ceil((double) x / k);
        for(p = 0; p <= k; p++) {
            q = (x - p * fl) / ce;
            if(p * fl + q * ce == x)
                break;
        }
        printf("%d %d\n", p, q);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/10462481.html