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

📅  最后修改于: 2021-04-22 06:49:24             🧑  作者: Mango

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

例子:

方法:该想法基于以下观察:

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

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

  • 如果给定的数字小于10,则打印数字本身。
  • 如果给定的数字大于45,则打印“ -1”
  • 否则,请执行以下步骤:
    • 初始化字符串res来存储所需的答案,并将变量(例如digit)初始化为9
    • 循环执行直到N≤数字并执行以下步骤:
      • 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)