数字和 K 且没有 0 或重复相邻数字的最大数
给定一个整数K ,任务是找到具有数字总和 K 且没有 0 且连续相同数字的最大可能数。
例子:
Input: K = 3
Output: 21
Explanation: All possible numbers giving digit sum 3 without zero are – 3, 12, 21, 111.
As 111 has consecutive 1s, it isn’t a valid answer.
Hence maximum number with digit sum 3 is 21.
Input: K = 4
Output: 121
Explanation: All possible numbers giving digit sum 4 without zero are – 4, 13, 22, 31, 112, 121, 211, 1111.
As 1111, 211, has consecutive 1s, it isn’t a valid answer.
Hence maximum number with digit sum 4 is 121.
方法:可以使用以下思路解决问题:
To get the maximum value it should have maximum number of digits. For that the least significant digits should be as minimum as possible without violating the conditions. So those can be filled in any of the following two ways:
- 1 then 2 repeatedly from the least significant position till the digit sum is K
- 2 and then 1 repeatedly from the least significant position till the digit sum is K
So to choose the proper sequence divide the sum by 3 and if the remainder is 1 or 0 then start filling with 1 from last, as the remaining sum will be 1 at end. Otherwise use 2nd sequence.
请按照以下步骤解决问题:
- 将给定的数字除以 3。
- 根据上述观察根据余数进行填充。
- 返回数字作为答案。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
int maxNum(int n)
{
// Dividing n by 3
if (n % 3 == 1) {
while (n > 0) {
// Printing 1s and 2s
// and subtracting from n
n -= 1;
cout << "1";
if (n > 0) {
n -= 2;
cout << "2";
}
}
}
else {
while (n > 0) {
// Printing 2s and 1s and
// subtracting from n
n -= 2;
cout << "2";
if (n > 0) {
n -= 1;
cout << "1";
}
}
}
}
// Driver code
int main()
{
int K = 4;
maxNum(K);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG {
static void maxNum(int n)
{
// Dividing n by 3
if (n % 3 == 1) {
while (n > 0) {
// Printing 1s and 2s
// and subtracting from n
n -= 1;
System.out.print("1");
if (n > 0) {
n -= 2;
System.out.print("2");
}
}
}
else {
while (n > 0) {
// Printing 2s and 1s and
// subtracting from n
n -= 2;
System.out.print("2");
if (n > 0) {
n -= 1;
System.out.print("1");
}
}
}
}
// Driver code
public static void main (String[] args) {
int K = 4;
maxNum(K);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python 3 implementation of the approach
def maxNum(n):
# Dividing n by 3
if (n % 3 == 1):
while (n > 0):
# Printing 1s and 2s
# and subtracting from n
n -= 1
print("1", end="")
if (n > 0):
n -= 2
print("2", end="")
else:
while (n > 0):
# Printing 2s and 1s and
# subtracting from n
n -= 2
print("2", end="")
if (n > 0):
n -= 1
print("1", end="")
# Driver code
if __name__ == "__main__":
K = 4
maxNum(K)
# This code is contriuted by ukasp.
C#
// C# implementation of the approach
using System;
class GFG {
static void maxNum(int n)
{
// Dividing n by 3
if (n % 3 == 1) {
while (n > 0) {
// Printing 1s and 2s
// and subtracting from n
n -= 1;
Console.Write("1");
if (n > 0) {
n -= 2;
Console.Write("2");
}
}
}
else {
while (n > 0) {
// Printing 2s and 1s and
// subtracting from n
n -= 2;
Console.Write("2");
if (n > 0) {
n -= 1;
Console.Write("1");
}
}
}
}
// Driver code
public static void Main()
{
int K = 4;
maxNum(K);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
121
时间复杂度: O(K)
辅助空间: O(1)