📌  相关文章
📜  由数字和为 N 的非重复数字组成的最小正数

📅  最后修改于: 2021-09-06 06:50:48             🧑  作者: Mango

给定一个正整数N ,任务是找到由不同数字组成的最小正数,其数字之和等于N 。如果不存在这样的数字,则打印“-1”

例子:

方法:这个想法基于以下观察:

  • 如果 N < 10:要求的答案是N本身。
  • 如果 N > 45:要求答案为-1 ,因为可以使用非重复数字组成的最小数字为 123456789,其数字总和为 45。
  • 否则:可以根据以下模式获得答案。

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

  • 如果给定的数字小于 10,则打印数字本身。
  • 如果给定的数字大于 45,则打印“-1”
  • 否则,请执行以下步骤:
    • 初始化一个字符串res以存储所需的答案并初始化一个变量,比如digit ,为9
    • 迭代一个循环直到N ≤ digit并执行以下步骤:
      • res开始处按数字
      • 数字递减N。
      • 数字1
    • 如果发现N的值大于0 ,则压入res开头的字符。
    • 完成以上步骤后,打印res的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find smallest positive
// number made up of non-repeating digits
// whose sum of its digits is equal to n
void result(int n)
{
    if (n > 45) {
 
        // No such number exists
        cout << -1;
        return;
    }
 
    // Stores the required answer
    string res;
 
    // Store the digit at unit's place
    int digit = 9;
 
    // Iterate until n > digit
    while (n > digit) {
 
        // Push digit at the start of res
        res = char('0' + digit) + res;
 
        // Decrement n by digit
        n -= digit;
 
        // Decrement digit by 1
        digit -= 1;
    }
 
    if (n > 0) {
 
        // Push the remaining number
        // as the starting digit
        res = char('0' + n) + res;
    }
 
    // Print the required number
    cout << res;
}
 
// Driver Code
int main()
{
    int N = 19;
 
    // Function Call
    result(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to find smallest positive
// number made up of non-repeating digits
// whose sum of its digits is equal to n
static void result(int n)
{
    if (n > 45)
    {
 
        // No such number exists
        System.out.print(-1);
        return;
    }
 
    // Stores the required answer
    String res="";
 
    // Store the digit at unit's place
    int digit = 9;
 
    // Iterate until n > digit
    while (n > digit)
    {
 
        // Push digit at the start of res
        res = (char)('0' + digit) + res;
 
        // Decrement n by digit
        n -= digit;
 
        // Decrement digit by 1
        digit -= 1;
    }
 
    if (n > 0)
    {
 
        // Push the remaining number
        // as the starting digit
        res = (char)('0' + n) + res;
    }
 
    // Print the required number
    System.out.print(res);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 19;
 
    // Function Call
    result(N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find smallest positive
# number made up of non-repeating digits
# whose sum of its digits is equal to n
def result(n):
     
    if (n > 45):
         
        # No such number exists
        print(-1, end = "")
        return
 
    # Stores the required answer
    res = ""
 
    # Store the digit at unit's place
    digit = 9
 
    # Iterate until n > digit
    while (n > digit):
         
        # Push digit at the start of res
        res = str(digit) + res
 
        # Decrement n by digit
        n -= digit
         
        # Decrement digit by 1
        digit -= 1
 
    if (n > 0):
 
        # Push the remaining number
        # as the starting digit
        res = str(n) + res
 
    # Print the required number
    print(res)
 
# Driver Code
if __name__ == '__main__':
     
    N = 19
 
    # Function Call
    result(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
      
// Function to find smallest positive
// number made up of non-repeating digits
// whose sum of its digits is equal to n
static void result(int n)
{
    if (n > 45)
    {
  
        // No such number exists
        Console.Write(-1);
        return;
    }
  
    // Stores the required answer
    string res = "";
  
    // Store the digit at unit's place
    int digit = 9;
  
    // Iterate until n > digit
    while (n > digit)
    {
  
        // Push digit at the start of res
        res = (char)('0' + digit) + res;
  
        // Decrement n by digit
        n -= digit;
  
        // Decrement digit by 1
        digit -= 1;
    }
  
    if (n > 0)
    {
  
        // Push the remaining number
        // as the starting digit
        res = (char)('0' + n) + res;
    }
  
    // Print the required number
    Console.Write(res);
}
  
// Driver Code
public static void Main()
{
    int N = 19;
  
    // Function Call
    result(N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
289

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