给定三个正整数X 、 Y和N ,使得Y < X ,任务是从范围[0, N] 中找到最大的数,其模数X等于Y模数X 。
例子:
Input: X = 10, Y = 5, N = 15
Output: 15
Explanation:
The value of 15 % 10 (= 5) and 5 % 10 (= 5) are equal.
Therefore, the required output is 15.
Input: X = 5, Y = 0, N = 4
Output: 0
方法:根据以下观察可以解决给定的问题:
- 由于Y小于X ,那么Y % X必须是Y 。因此,想法是从范围[0, N] 中找到最大值,其与X的模数为Y。
- 假设最大数,例如num = N ,以得到X为Y的余数模。
- 用N % X的余数减去N得到余数为0 ,然后将Y添加到它。然后,该数字与X的余数将是Y 。
- 检查数字是否小于N 。如果发现为真,则设置num = (N – N % X + Y) 。
- 否则,再次减去具有X值的数字,即num = (N – N % X – (X – Y)) ,以获得区间[0, N] 中的最大值。
- 数学上:
- 如果(N – N % X + Y) ≤ N ,则设置num = (N – N % X + Y) 。
- 否则,更新num = (N – N % X – (X – Y)) 。
请按照以下步骤解决问题:
- 初始化一个变量,比如num ,以存储在[0, N]范围内具有余数Y % X的最大数字。
- 如果(N – N % X + Y) ≤ N ,则更新num = (N – N % X + Y) 。
- 否则,更新num = (N – N % X – (X – Y)) 。
- 完成以上步骤后,打印num的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the largest
// number upto N whose modulus
// with X is same as Y % X
long long maximumNum(long long X,
long long Y,
long long N)
{
// Stores the required number
long long num = 0;
// Update num as the result
if (N - N % X + Y <= N) {
num = N - N % X + Y;
}
else {
num = N - N % X - (X - Y);
}
// Return the resultant number
return num;
}
// Driver Code
int main()
{
long long X = 10;
long long Y = 5;
long long N = 15;
cout << maximumNum(X, Y, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
// Function to print the largest
// number upto N whose modulus
// with X is same as Y % X
static long maximumNum(long X, long Y, long N)
{
// Stores the required number
long num = 0;
// Update num as the result
if (N - N % X + Y <= N)
{
num = N - N % X + Y;
}
else
{
num = N - N % X - (X - Y);
}
// Return the resultant number
return num;
}
// Driver Code
public static void main(String[] args)
{
long X = 10;
long Y = 5;
long N = 15;
System.out.println(maximumNum(X, Y, N));
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to print the largest
# number upto N whose modulus
# with X is same as Y % X
def maximumNum(X, Y, N):
# Stores the required number
num = 0
# Update num as the result
if (N - N % X + Y <= N):
num = N - N % X + Y
else:
num = N - N % X - (X - Y)
# Return the resultant number
return num
# Driver Code
if __name__ == '__main__':
X = 10
Y = 5
N = 15
print (maximumNum(X, Y, N))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG {
// Function to print the largest
// number upto N whose modulus
// with X is same as Y % X
static long maximumNum(long X, long Y, long N)
{
// Stores the required number
long num = 0;
// Update num as the result
if (N - N % X + Y <= N) {
num = N - N % X + Y;
}
else {
num = N - N % X - (X - Y);
}
// Return the resultant number
return num;
}
// Driver Code
public static void Main(string[] args)
{
long X = 10;
long Y = 5;
long N = 15;
Console.WriteLine(maximumNum(X, Y, N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
15
时间复杂度: O(1)
辅助空间: O(1)