给定两个正整数A和B ,其中A大于B。一次移动可以使A减小1,这意味着一次移动后A等于A – 1。 B在恒定时间内可被B整除。
例子:
Input : A = 10, B = 3
Output : 1
Explanation:
Only one move is required A = A - 1 = 9,
which is divisible by 3.
Input : A = 10, B = 10
Output : 0
Explanation:
Since A is equal to B therefore zero move required.
方法:
为了解决上述问题,我们采用A%B的数字模,并将结果存储在变量中,该变量是必需的答案。
下面是上述方法的实现:
C++
// C++ implementation to count
// Total numbers moves to make
// integer A divisible by integer B
#include
using namespace std;
// Function that print number
// of moves required
void movesRequired(int a, int b)
{
// calculate modulo
int total_moves = a % b;
// print the required answer
cout << total_moves << "\n";
}
// Driver Code
int main()
{
// initialise A and B
int A = 10, B = 3;
movesRequired(A, B);
return 0;
}
Java
// Java implementation to count
// total numbers moves to make
// integer A divisible by integer B
import java.util.*;
class GFG{
// Function that print number
// of moves required
static void movesRequired(int a, int b)
{
// Calculate modulo
int total_moves = a % b;
// Print the required answer
System.out.println(total_moves);
}
// Driver code
public static void main(String[] args)
{
// Initialise A and B
int A = 10, B = 3;
movesRequired(A, B);
}
}
// This code is contributed by offbeat
Python3
# Python3 implementation to count
# total numbers moves to make
# integer A divisible by integer B
# Function that print number
# of moves required
def movesRequired(a, b):
# Calculate modulo
total_moves = a % b
# Print the required answer
print(total_moves)
# Driver Code
if __name__ == '__main__':
# Initialise A and B
A = 10
B = 3
movesRequired(A, B)
# This code is contributed by Samarth
C#
// C# implementation to count
// total numbers moves to make
// integer A divisible by integer B
using System;
class GFG
{
// Function that print number
// of moves required
static void movesRequired(int a, int b)
{
// Calculate modulo
int total_moves = a % b;
// Print the required answer
Console.Write(total_moves);
}
// Driver code
public static void Main(String []args)
{
// Initialise A and B
int A = 10, B = 3;
movesRequired(A, B);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
1