📜  最多N的最大数,其与X的模数等于X的Y模

📅  最后修改于: 2021-04-17 16:51:44             🧑  作者: Mango

给定三个正整数XYN ,使得Y ,任务是从范围[0,N]中找到最大数,该范围的模数与X等于YX。

例子:

方法:可以根据以下观察结果解决给定问题:

  • 由于Y小于X ,则Y%X必须为Y。因此,从具有X的模量为Y的范围[0,N]中找到最大值的想法。
  • 假定最大数量,例如num = N ,以XY得到余数模。
  • 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 ,以存储具有[ Y,X ]范围内的余数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.


输出:
15

时间复杂度: O(1)
辅助空间: O(1)