📜  数字和 K 且没有 0 或重复相邻数字的最大数

📅  最后修改于: 2022-05-13 01:56:07.426000             🧑  作者: Mango

数字和 K 且没有 0 或重复相邻数字的最大数

给定一个整数K ,任务是找到具有数字总和 K 且没有 0 且连续相同数字的最大可能数。

例子:

方法:可以使用以下思路解决问题:

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

  • 将给定的数字除以 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)