给定正整数n 。问题是要打印1到n范围内具有交替模式位的数字。在此,交替模式表示编号中的置位和未置位以交替顺序出现。例如-5具有替代图案,即101。
例子:
Input : n = 10
Output : 1 2 5 10
Input : n = 50
Output : 1 2 5 10 21 42
方法1(天真方法):生成1到n范围内的所有数字,并针对每个生成的数字检查其是否具有交替模式的位。时间复杂度为O(n)。
方法2(有效方法):算法:
printNumHavingAltBitPatrn(n)
Initialize curr_num = 1
print curr_num
while (1)
curr_num <<= 1
if n < curr_num then
break
print curr_num
curr_num = ((curr_num) << 1) ^ 1
if n < curr_num then
break
print curr_num
CPP
// C++ implementation to print numbers in the range
// 1 to n having bits in alternate pattern
#include
using namespace std;
// function to print numbers in the range 1 to n
// having bits in alternate pattern
void printNumHavingAltBitPatrn(int n)
{
// first number having bits in alternate pattern
int curr_num = 1;
// display
cout << curr_num << " ";
// loop until n < curr_num
while (1) {
// generate next number having alternate
// bit pattern
curr_num <<= 1;
// if true then break
if (n < curr_num)
break;
// display
cout << curr_num << " ";
// generate next number having alternate
// bit pattern
curr_num = ((curr_num) << 1) ^ 1;
// if true then break
if (n < curr_num)
break;
// display
cout << curr_num << " ";
}
}
// Driver program to test above
int main()
{
int n = 50;
printNumHavingAltBitPatrn(n);
return 0;
}
Java
// Java implementation to print numbers in the range
// 1 to n having bits in alternate pattern
import java.io.*;
import java.util.*;
class GFG
{
public static void printNumHavingAltBitPatrn(int n)
{
// first number having bits in alternate pattern
int curr_num = 1, i = 1;
// display
System.out.print(curr_num + " ");
// loop until n < curr_num
while (i!=0)
{
i++;
// generate next number having alternate
// bit pattern
curr_num <<= 1;
// if true then break
if (n < curr_num)
break;
// display
System.out.print(curr_num + " ");
// generate next number having alternate
// bit pattern
curr_num = ((curr_num) << 1) ^ 1;
// if true then break
if (n < curr_num)
break;
// display
System.out.print(curr_num + " ");
}
}
public static void main (String[] args)
{
int n = 50;
printNumHavingAltBitPatrn(n);
}
}
// Code Contributed by Mohit Gupta_OMG <(0_o)>
Python3
# Python3 program for count total
# zero in product of array
# function to print numbers in the range
# 1 to nhaving bits in alternate pattern
def printNumHavingAltBitPatrn(n):
# first number having bits in
# alternate pattern
curr_num = 1
# display
print (curr_num)
# loop until n < curr_num
while (1) :
# generate next number having
# alternate bit pattern
curr_num = curr_num << 1;
# if true then break
if (n < curr_num):
break;
# display
print( curr_num )
# generate next number having
# alternate bit pattern
curr_num = ((curr_num) << 1) ^ 1;
# if true then break
if (n < curr_num):
break
# display
print( curr_num )
# Driven code
n = 50
printNumHavingAltBitPatrn(n)
# This code is contributed by "rishabh_jain".
C#
// C# implementation to print numbers in the range
// 1 to n having bits in alternate pattern
using System;
class GFG {
// function to print numbers in the range 1 to n
// having bits in alternate pattern
public static void printNumHavingAltBitPatrn(int n)
{
// first number having bits in alternate pattern
int curr_num = 1, i = 1;
// display
Console.Write(curr_num + " ");
// loop until n < curr_num
while (i!=0)
{
// generate next number having alternate
// bit pattern
curr_num <<= 1;
// if true then break
if (n < curr_num)
break;
// display
Console.Write(curr_num + " ");
// generate next number having alternate
// bit pattern
curr_num = ((curr_num) << 1) ^ 1;
// if true then break
if (n < curr_num)
break;
// display
Console.Write(curr_num + " ");
}
}
// Driver code
public static void Main ()
{
int n = 50;
printNumHavingAltBitPatrn(n);
}
}
// This code is contributed by Sam007.
PHP
Javascript
输出:
1 2 5 10 21 42