给定一个正整数N ,任务是找到由不同数字组成的最大正数,其数字之和等于N 。如果不存在这样的数字,则打印“-1” 。
例子:
Input: N = 25
Output: 98710
Explanation:
The number 98710 is the largest number that contains only unique digits and the sum of its digits (9 + 8 + 7 + 1 + 0) is N(= 25).
Input: N = 50
Output: -1
方法:根据以下观察可以解决给定的问题:
- 如果 N 的值至少为 45:所需的结果是-1 ,因为可以使用非重复数字组成的最大数字是9876543210 ,其数字总和为45 。
- 对于 N 的所有其他值:要获得最大的数字,从最大的数字开始,即9 ,并不断递减它并将其添加到所需的数字。所需的数字必须在其末尾包含0 ,因为它会增加值而不更改数字总和。
请按照以下步骤解决问题:
- 如果给定的数字N大于45 ,则打印“-1” 。
- 否则,请执行以下步骤:
- 初始化一个变量,比如num为0来存储所需的结果,以及一个变量,比如digit ,为9 。
- 迭代一个循环,直到N和digit的值为正 并执行以下步骤:
- 如果一个数字的值是至多N,由10然后乘以NUM,然后数字的值添加到Num和由数字递减的N的值。
- 此外,将数字的值减少1 。
- 将变量num乘以10并在末尾附加数字0 。
- 完成上述步骤后,将num的值打印为结果数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the largest positive
// number made up of distinct digits
// having the sum of its digits as N
long largestNumber(int N)
{
// If given number is greater
// than 45, print -1
if (N > 45)
return -1;
// Store the required number and
// the digit to be considered
int num = 0, digit = 9;
// Loop until N > 0 and digit > 0
while (N > 0 && digit > 0) {
// If the current digit is
// at most N then, add it
// to number num
if (digit <= N) {
// Update the value of
// num
num *= 10;
num += digit;
// Decrement N by digit
N -= digit;
}
// Consider the next lower
// digit
digit -= 1;
}
// Add 0 at the end and return
// the number num
return num * 10;
}
// Driver Code
int main()
{
int N = 25;
cout << largestNumber(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to find the largest positive
// number made up of distinct digits
// having the sum of its digits as N
static long largestNumber(int N)
{
// If given number is greater
// than 45, print -1
if (N > 45)
return -1;
// Store the required number and
// the digit to be considered
int num = 0, digit = 9;
// Loop until N > 0 and digit > 0
while (N > 0 && digit > 0)
{
// If the current digit is
// at most N then, add it
// to number num
if (digit <= N)
{
// Update the value of
// num
num *= 10;
num += digit;
// Decrement N by digit
N -= digit;
}
// Consider the next lower
// digit
digit -= 1;
}
// Add 0 at the end and return
// the number num
return num * 10;
}
// Driver Code
public static void main (String[] args)
{
int N = 25;
System.out.print(largestNumber(N));
}
}
// This code is contributed by sanjoy_62
Python3
# Python program for the above approach
# Function to find the largest positive
# number made up of distinct digits
# having the sum of its digits as N
def largestNumber(N):
# If given number is greater
# than 45, print -1
if (N > 45):
return -1
# Store the required number and
# the digit to be considered
num = 0
digit = 9
# Loop until N > 0 and digit > 0
while (N > 0 and digit > 0):
# If the current digit is
# at most N then, add it
# to number num
if (digit <= N):
# Update the value of
# num
num *= 10
num += digit
# Decrement N by digit
N -= digit
# Consider the next lower
# digit
digit -= 1
# Add 0 at the end and return
# the number num
return num * 10
# Driver Code
N = 25
print(largestNumber(N))
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the largest positive
// number made up of distinct digits
// having the sum of its digits as N
static long largestNumber(int N)
{
// If given number is greater
// than 45, print -1
if (N > 45)
return -1;
// Store the required number and
// the digit to be considered
int num = 0, digit = 9;
// Loop until N > 0 and digit > 0
while (N > 0 && digit > 0)
{
// If the current digit is
// at most N then, add it
// to number num
if (digit <= N)
{
// Update the value of
// num
num *= 10;
num += digit;
// Decrement N by digit
N -= digit;
}
// Consider the next lower
// digit
digit -= 1;
}
// Add 0 at the end and return
// the number num
return num * 10;
}
// Driver Code
public static void Main()
{
int N = 25;
Console.Write(largestNumber(N));
}
}
// This code is contributed by ukasp
Javascript
输出:
98710
时间复杂度: O(1)
辅助空间: O(1)