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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章