📜  找到给定数字和数字总和的最小数字

📅  最后修改于: 2021-10-26 06:26:14             🧑  作者: Mango

给定两个正整数PQ ,找到只包含数字PQ的最小整数,使得整数的数字之和为N

例子:

高效方法:让我们考虑P大于或等于Qcount_P表示P的出现次数, count_Q表示Q在结果整数中出现的次数。因此,问题可以用等式 ( P * count_P) + (Q * count_Q) = N 的形式表示,并且为了最小化结果整数中的位数, count_P + count_Q应该尽可能小。可以观察到,由于P >= Q ,满足 ( P * count_P) + (Q * count_Q) = Ncount_P的最大可能值将是最优选择。以下是上述方法的步骤:

  1. count_Pcount_Q初始化为 0。
  2. 如果N可被P整除,则count_P = N/PN=0
  3. 如果N不能被P整除,则从N 中减去Q并将count_Q增加 1。
  4. 重复第 2 步和第 3 步,直到N大于 0。
  5. 如果N != 0 ,则无法生成满足所需条件的整数。否则,结果整数将是count_Q 乘以 Q,然后是count_P 乘以 P。

下面是上述方法的实现:

C++
// C++ Program of the above approach
#include 
using namespace std;
 
// Function to print the minimum
// integer having only digits P and
// Q and the sum of digits as N
void printMinInteger(int P, int Q, int N)
{
    // If Q is greater that P then
    // swap the values of P and Q
    if (Q > P) {
        swap(P, Q);
    }
 
    // If P and Q are both zero or
    // if Q is zero and N is not
    // divisible by P then there
    // is no possible integer which
    // satisfies the given conditions
    if (Q == 0 && (P == 0 || N % P != 0)) {
        cout << "Not Possible";
        return;
    }
 
    int count_P = 0, count_Q = 0;
 
    // Loop to find the maximum value
    // of count_P that also satisfy
    // P*count_P + Q*count_Q = N
    while (N > 0) {
        if (N % P == 0) {
            count_P += N / P;
            N = 0;
        }
        else {
            N = N - Q;
            count_Q++;
        }
    }
 
    // If N is 0, their is a valid
    // integer possible that satisfies
    // all the requires conditions
    if (N == 0) {
 
        // Print Answer
        for (int i = 0; i < count_Q; i++)
            cout << Q;
        for (int i = 0; i < count_P; i++)
            cout << P;
    }
    else {
        cout << "Not Possible";
    }
}
 
// Driver Code
int main()
{
    int N = 32;
    int P = 7;
    int Q = 4;
 
    // Function Call
    printMinInteger(P, Q, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
// Function to print the minimum
// integer having only digits P and
// Q and the sum of digits as N
static void printMinInteger(int P, int Q, int N)
{
   
    // If Q is greater that P then
    // swap the values of P and Q
    if (Q > P) {
        int temp;
        temp = P;
        P = Q;
        Q = temp;
    }
 
    // If P and Q are both zero or
    // if Q is zero and N is not
    // divisible by P then there
    // is no possible integer which
    // satisfies the given conditions
    if (Q == 0 && (P == 0 || N % P != 0)) {
        System.out.println("Not Possible");
        return;
    }
 
    int count_P = 0, count_Q = 0;
 
    // Loop to find the maximum value
    // of count_P that also satisfy
    // P*count_P + Q*count_Q = N
    while (N > 0) {
        if (N % P == 0) {
            count_P += N / P;
            N = 0;
        }
        else {
            N = N - Q;
            count_Q++;
        }
    }
 
    // If N is 0, their is a valid
    // integer possible that satisfies
    // all the requires conditions
    if (N == 0) {
 
        // Print Answer
        for (int i = 0; i < count_Q; i++)
            System.out.print(Q);
        for (int i = 0; i < count_P; i++)
            System.out.print(P);
    }
    else {
        System.out.println("Not Possible");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 32;
    int P = 7;
    int Q = 4;
 
    // Function Call
    printMinInteger(P, Q, N);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
 
# Function to prthe minimum
# integer having only digits P and
# Q and the sum of digits as N
def printMinInteger(P, Q, N):
     
    # If Q is greater that P then
    # swap the values of P and Q
    if (Q > P):
        t = P
        P = Q
        Q = t
 
    # If P and Q are both zero or
    # if Q is zero and N is not
    # divisible by P then there
    # is no possible integer which
    # satisfies the given conditions
    if (Q == 0 and (P == 0 or N % P != 0)):
        print("Not Possible")
        return
     
    count_P = 0
    count_Q = 0
 
    # Loop to find the maximum value
    # of count_P that also satisfy
    # P*count_P + Q*count_Q = N
    while (N > 0):
        if (N % P == 0):
            count_P += N / P
            N = 0
        else:
            N = N - Q
            count_Q += 1
         
    # If N is 0, their is a valid
    # integer possible that satisfies
    # all the requires conditions
    if (N == 0):
         
        # Print Answer
        for i in range(count_Q):
            print(Q, end = "")
        for i in range(int(count_P)):
            print(P, end = "")
    else:
        print("Not Possible")
     
# Driver Code
N = 32
P = 7
Q = 4
 
# Function Call
printMinInteger(P, Q, N)
 
# This code is contributed by code_hunt


C#
// C# program for the above approach
using System;
 
public class GFG {
 
// Function to print the minimum
// integer having only digits P and
// Q and the sum of digits as N
static void printMinint(int P, int Q, int N)
{
   
    // If Q is greater that P then
    // swap the values of P and Q
    if (Q > P) {
        int temp;
        temp = P;
        P = Q;
        Q = temp;
    }
 
    // If P and Q are both zero or
    // if Q is zero and N is not
    // divisible by P then there
    // is no possible integer which
    // satisfies the given conditions
    if (Q == 0 && (P == 0 || N % P != 0)) {
        Console.WriteLine("Not Possible");
        return;
    }
 
    int count_P = 0, count_Q = 0;
 
    // Loop to find the maximum value
    // of count_P that also satisfy
    // P*count_P + Q*count_Q = N
    while (N > 0) {
        if (N % P == 0) {
            count_P += N / P;
            N = 0;
        }
        else {
            N = N - Q;
            count_Q++;
        }
    }
 
    // If N is 0, their is a valid
    // integer possible that satisfies
    // all the requires conditions
    if (N == 0) {
 
        // Print Answer
        for (int i = 0; i < count_Q; i++)
            Console.Write(Q);
        for (int i = 0; i < count_P; i++)
            Console.Write(P);
    }
    else {
        Console.WriteLine("Not Possible");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 32;
    int P = 7;
    int Q = 4;
 
    // Function Call
    printMinint(P, Q, N);
}
}
 
// This code contributed by shikhasingrajput


Javascript


输出
47777

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

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