HOJ 3120 (A-B)%C

Description

We have 3 positive numbers a, b, c where a >= b. Given p = a mod c and q = b mod c, you should calculate ( a - b) mod c.


Hint 1: For any two positive numbers a and b, where a = k * b + n, 0 <= n < b, we have a mod b = n.
Hint 2: Use % to calculate a mod b in C. Like x = a % b;
Hint 3: For positive numbers a, b, c where a >= b, it doesn't always hold that ( a mod c) >= ( b mod c).


Input

Multiple test cases.
The first line of each input is an number N, which means that N lines follow, one case per line.
In each line we have 3 positive numbers p, q, c as discribed above, where p, q, c <= 10^7.


Output

For each test case, output ( a - b) mod c in one line.

Sample input

3
5 3 8
6 8 10
3 3 4

Sample output

2
8
0

Solution:

#include <iostream>

using namespace std;

int main()
{
    int num;
    int p,q,a,b,c,m;

    cin >> num;
    for(int i=0;i<num;i++)
    {
        cin >> p >> q >> c;
        while(p <= 0 || q <= 0 || c <= 0)
        {
            cout << "please input positive numbers:" << endl;
            cin >> p >> q >> c;
        }
        m = 1;
        while(1){
            a = m * c + p;
            b = c + q;
            if(a < b) m++;
            else break;
        }
        cout << (a-b)%c << endl;
    }
    return 0;
}
 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值