'How to calculate minimum number of operations to make a number divisible by another
I have a coding prompt I am trying to solve:
You are given two positive integers
a
andb
. In one move you can increasea
by 1 (replacea
witha+1
). Your task is to find the minimum number of moves you need to do in order to makea
divisible byb
. It is possible that you have to make 0 moves, asa
is already divisible byb
. You have to answert
independent test cases.
Input: The first line of the input contains
t(1<t<10000)
- the number of test cases thent
test case follows the only line of the test case contains two integersa,b(1<=a,b<=10^9)
Output: For each test case print the answer the minimum number of moves you need to do in order to
a
divisible byb
Here is my code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long int t, a, b;
int value, count = 0, i, j;
scanf("%lld", &t);
if (t >= 1 && t <= pow(10, 4))
{
for (j = 1; j <= t; j++)
{
scanf("%lld %lld", &a, &b);
if ((a >= 1 && a <= pow(10, 9)) && (b >= 1 && b <= pow(10, 9)))
{
value = a;
for (i = 1; a % b != 0; i++)
{
count++;
a = (value + i);
}
printf("%d\n", count);
a = 0;
b = 0;
count = 0;
}
}
}
return 0;
}
Solution 1:[1]
( With How do I ask and answer homework questions? in mind I turn my comment into the first step of an answer according to the compromise desribed there. )
Hint 1:
Output the input values and a % b
(in case of hidden test cases just invent a few). What does that value tell you about the number of moves you will need?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yunnosch |