原文:
给定正整数 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 范围内的所有数字,对于每个生成的数字。时间复杂度为 0(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
卡片打印处理机(card print processor 的缩写)
// 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)>
python 3
# 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.
服务器端编程语言(professional hypertext preprocessor 的缩写)
java 描述语言
输出:
1 2 5 10 21 42
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处