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

📅  最后修改于: 2021-05-04 20:52:05             🧑  作者: Mango

给定三个整数XYN ,任务是找到仅由XY组成的最大长度N的数字,这样X的计数可以被Y整除,反之亦然。如果无法形成这样的数字,请打印-1

例子:

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

  • XY中的较大者视为X并将较小者视为Y。
  • 由于该数字的长度必须为N ,请执行以下两个步骤,直到N≤0:
    • 如果N可被Y整除,则将N次附加X次,并将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


输出:
7777755555555555555

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