📌  相关文章
📜  生成仅由1或2组成的N位数字,该数字可以被2N整除

📅  最后修改于: 2021-04-17 19:02:22             🧑  作者: Mango

给定一个整数N,则任务是产生一个N位数字号码,其仅包含数字12,2 N整除。

例子:

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

  • 迭代[1,2 N ]范围内的所有值。
  • 对于该范围内的每个整数i ,使用位集生成其二进制表示形式并将其存储在字符串,例如S。
  • 将字符串S的长度减少到N。
  • 遍历S的位,如果S i ==’0′ ,则设置S i =’2’。
  • 将获得的字符串转换为整数,例如使用stoll()的res
  • 如果res可被2 N整除,则打印整数并中断。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Utility function to find x^y in O(log(y))
long long int power(long long int x,
                    unsigned long long int y)
{
    // Stores the result
    long long int res = 1;
 
    // Update x if it is >= p
    while (y > 0) {
        // If y is odd
        if (y & 1)
 
            // Multiply x with res
            res = (res * x);
 
        // y must be even now
        // Set y = y/2
        y = y >> 1;
        x = (x * x);
    }
    return res;
}
 
// Function to generate the N digit number
// satisfying the given conditions
void printNth(int N)
{
    // Find all possible integers upto 2^N
    for (long long int i = 1;
         i <= power(2, N); i++) {
 
        // Generate binary representation of i
        string s = bitset<64>(i).to_string();
 
        // Reduce the length of the string to N
        string s1 = s.substr(
            s.length() - N, s.length());
 
        for (long long int j = 0; s1[j]; j++) {
 
            // If current bit is '0'
            if (s1[j] == '0') {
                s1[j] = '2';
            }
        }
 
        // Convert string to equivalent integer
        long long int res = stoll(s1, nullptr, 10);
 
        // If condition satisfies
        if (res % power(2, N) == 0) {
 
            // Print and break
            cout << res << endl;
            break;
        }
    }
}
 
// Driver Code
int main()
{
    int N = 15;
    printNth(N);
}


Python3
# Python program for the above approach
 
# Utility function to find x^y in O(log(y))
def power(x, y):
     
    # Stores the result
    res = 1
     
    # Update x if it is >= p
    while (y > 0):
       
        # If y is odd
        if (y & 1):
             
            # Multiply x with res
            res = (res * x)
             
        # y must be even now
        # Set y = y/2
        y = y >> 1
        x = (x * x)
    return res
 
# Function to generate the N digit number
# satisfying the given conditions
def printNth(N):
     
    # Find all possible integers upto 2^N
    for i in range(1,power(2, N) + 1):
         
        # Generate binary representation of i
        s = "{:064b}".format(i)
         
        # Reduce the length of the string to N
        s1 = s[len(s)- N: 2*len(s)-N]
         
        j = 0
        while(j < len(s1)):
           
            # If current bit is '0'
            if (s1[j] == '0'):
                s1 = s1[:j] + '2' + s1[j + 1:]
            j += 1
         
        # Convert string to equivalent integer
        res = int(s1)
         
        # If condition satisfies
        if (res % power(2, N) == 0):
             
            # Prand break
            print(res)
            break
 
# Driver Code
N = 15
printNth(N)
 
# This code is contributed by shubhamsingh10


输出:
211111212122112

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