📌  相关文章
📜  由 X 和 Y 组成的最大数,其中 X 可被 Y 整除,Y 可被 X 整除

📅  最后修改于: 2021-10-26 02:32:01             🧑  作者: Mango

给定三个整数XYN ,任务是找到长度为N的最大可能数,仅由XY作为其数字组成,这样,其中X的计数可以被Y整除,反之亦然.如果不能形成这样的数字,则打印-1
例子:

方法:
请按照以下步骤解决问题:

  • XY 中较大的视为X ,将较小的视为Y
  • 由于数字需要长度为N ,因此执行以下两个步骤直到 N ≤ 0:
    • 如果 N 可被 Y 整除,则将 X 附加到答案中 N 次并将 N 减少为零。
    • 否则,将 N 减少 X 并将 Y、X 次附加到答案。
  • 完成上述步骤后,如果N<0,则需要的多个类型是不可能的。打印-1
  • 否则,打印答案。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to generate and return
// the largest number
void largestNumber(int n, int X, int Y)
{
    int maxm = max(X, Y);
 
    // Store the smaller in Y
    Y = X + Y - maxm;
 
    // Store the larger in X
    X = maxm;
 
    // Stores respective counts
    int Xs = 0;
    int Ys = 0;
 
    while (n > 0) {
 
        // If N is divisible by Y
        if (n % Y == 0) {
 
            // Append X, N times to
            // the answer
            Xs += n;
 
            // Reduce N to zero
            n = 0;
        }
        else {
 
            // Reduce N by X
            n -= X;
 
            // Append Y, X times
            // to the answer
            Ys += X;
        }
    }
 
    // If number can be formed
    if (n == 0) {
        while (Xs-- > 0)
            cout << X;
 
        while (Ys-- > 0)
            cout << Y;
    }
 
    // Otherwise
    else
        cout << "-1";
}
 
// Driver Code
int main()
{
    int n = 19, X = 7, Y = 5;
    largestNumber(n, X, Y);
    return 0;
}


Java
// Java program to implement the
// above approach
import java.util.*;
 
class GFG{
     
// Function to generate and return
// the largest number
public static void largestNumber(int n, int X,
                                        int Y)
{
    int maxm = Math.max(X, Y);
     
    // Store the smaller in Y
    Y = X + Y - maxm;
     
    // Store the larger in X
    X = maxm;
     
    // Stores respective counts
    int Xs = 0;
    int Ys = 0;
     
    while (n > 0)
    {
         
        // If N is divisible by Y
        if (n % Y == 0)
        {
             
            // Append X, N times to
            // the answer
            Xs += n;
     
            // Reduce N to zero
            n = 0;
        }
        else
        {
            // Reduce N by X
            n -= X;
     
            // Append Y, X times
            // to the answer
            Ys += X;
        }
    }
     
    // If number can be formed
    if (n == 0)
    {
        while (Xs-- > 0)
            System.out.print(X);
     
        while (Ys-- > 0)
            System.out.print(Y);
    }
     
    // Otherwise
    else
        System.out.print("-1");
}
 
// Driver code
public static void main (String[] args)
{
    int n = 19, X = 7, Y = 5;
     
    largestNumber(n, X, Y);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to implement
# the above approach
 
# Function to generate and return
# the largest number
def largestNumber(n, X, Y):
 
    maxm = max(X, Y)
 
    # Store the smaller in Y
    Y = X + Y - maxm
 
    # Store the larger in X
    X = maxm
 
    # Stores respective counts
    Xs = 0
    Ys = 0
 
    while (n > 0):
 
        # If N is divisible by Y
        if (n % Y == 0):
 
            # Append X, N times to
            # the answer
            Xs += n
 
            # Reduce N to zero
            n = 0
 
        else:
             
            # Reduce N by x
            n -= X
 
            # Append Y, X times to
            # the answer
            Ys += X
 
    # If number can be formed
    if (n == 0):
         
        while (Xs > 0):
            Xs -= 1
            print(X, end = '')
             
        while (Ys > 0):
            Ys -= 1
            print(Y, end = '')
 
    # Otherwise
    else:
        print("-1")
 
# Driver code
n = 19
X = 7
Y = 5
 
largestNumber(n, X, Y)
 
# This code is contributed by himanshu77


C#
// C# program to implement the
// above approach
using System;
class GFG{
     
// Function to generate and return
// the largest number
public static void largestNumber(int n, int X,
                                        int Y)
{
    int maxm = Math.Max(X, Y);
     
    // Store the smaller in Y
    Y = X + Y - maxm;
     
    // Store the larger in X
    X = maxm;
     
    // Stores respective counts
    int Xs = 0;
    int Ys = 0;
     
    while (n > 0)
    {
         
        // If N is divisible by Y
        if (n % Y == 0)
        {
             
            // Append X, N times to
            // the answer
            Xs += n;
     
            // Reduce N to zero
            n = 0;
        }
        else
        {
            // Reduce N by X
            n -= X;
     
            // Append Y, X times
            // to the answer
            Ys += X;
        }
    }
     
    // If number can be formed
    if (n == 0)
    {
        while (Xs-- > 0)
            Console.Write(X);
     
        while (Ys-- > 0)
            Console.Write(Y);
    }
     
    // Otherwise
    else
        Console.Write("-1");
}
 
// Driver code
public static void Main (String[] args)
{
    int n = 19, X = 7, Y = 5;
     
    largestNumber(n, X, Y);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
7777755555555555555

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程