给定一个n> 0的整数,任务是查找连续1的整数计数的位模式是否从左向右递增。
例子 :
Input:19
Output:Yes
Explanation: Bit-pattern of 19 = 10011,
Counts of continuous 1's from left to right
are 1, 2 which are in increasing order.
Input : 183
Output : yes
Explanation: Bit-pattern of 183 = 10110111,
Counts of continuous 1's from left to right
are 1, 2, 3 which are in increasing order.
一个简单的解决方案是将给定数字的二进制表示形式存储到字符串,然后从左向右遍历并计算连续1的数量。对于每次遇到的0,请检查连续1的前一个计数到当前值的计数,如果前一个计数的值大于当前计数的值,则返回False,否则当字符串结束时返回True。
C++
// C++ program to find if bit-pattern
// of a number has increasing value of
// continuous-1 or not.
#include
using namespace std;
// Returns true if n has increasing count of
// continuous-1 else false
bool findContinuous1(int n)
{
const int bits = 8*sizeof(int);
// store the bit-pattern of n into
// bit bitset- bp
string bp = bitset (n).to_string();
// set prev_count = 0 and curr_count = 0.
int prev_count = 0, curr_count = 0;
int i = 0;
while (i < bits)
{
if (bp[i] == '1')
{
// increment current count of continuous-1
curr_count++;
i++;
}
// traverse all continuous-0
else if (bp[i-1] == '0')
{
i++;
curr_count = 0;
continue;
}
// check prev_count and curr_count
// on encounter of first zero after
// continuous-1s
else
{
if (curr_count < prev_count)
return 0;
i++;
prev_count=curr_count;
curr_count = 0;
}
}
// check for last sequence of continuous-1
if (prev_count > curr_count && (curr_count != 0))
return 0;
return 1;
}
// Driver code
int main()
{
int n = 179;
if (findContinuous1(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Python3
# Python3 program to find if bit-pattern
# of a number has increasing value of
# continuous-1 or not.
# Returns true if n has increasing count of
# continuous-1 else false
def findContinuous1(n):
# store the bit-pattern of n into
# bit bitset- bp
bp = list(bin(n))
bits = len(bp)
# set prev_count = 0 and curr_count = 0.
prev_count = 0
curr_count = 0
i = 0
while (i < bits):
if (bp[i] == '1'):
# increment current count of continuous-1
curr_count += 1
i += 1
# traverse all continuous-0
elif (bp[i - 1] == '0'):
i += 1
curr_count = 0
continue
# check prev_count and curr_count
# on encounter of first zero after
# continuous-1s
else:
if (curr_count < prev_count):
return 0
i += 1
prev_count = curr_count
curr_count = 0
# check for last sequence of continuous-1
if (prev_count > curr_count and (curr_count != 0)):
return 0
return 1
# Driver code
n = 179
if (findContinuous1(n)):
print( "Yes")
else:
print( "No")
# This code is contributed by SHUBHAMSINGH10
C++
// C++ program to check if counts of consecutive
// 1s are increasing order.
#include
using namespace std;
// Returns true if n has counts of consecutive
// 1's are increasing order.
bool areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = INT_MAX;
// We traverse bits from right to left
// and check if counts are decreasing
// order.
while (n > 0)
{
// Ignore 0s until we reach a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;
// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}
// Compare current with previous and
// update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}
return true;
}
// Driver code
int main()
{
int n = 10;
if (areSetBitsIncreasing(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program to check if counts of
// consecutive 1s are increasing order.
import java .io.*;
class GFG {
// Returns true if n has counts of
// consecutive 1's are increasing
// order.
static boolean areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = Integer.MAX_VALUE;
// We traverse bits from right to
// left and check if counts are
// decreasing order.
while (n > 0)
{
// Ignore 0s until we reach
// a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;
// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}
// Compare current with previous
// and update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}
return true;
}
// Driver code
static public void main (String[] args)
{
int n = 10;
if (areSetBitsIncreasing(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by anuj_67.
Python3
# Python3 program to check if counts of
# consecutive 1s are increasing order.
import sys
# Returns true if n has counts of
# consecutive 1's are increasing order.
def areSetBitsIncreasing(n):
# Initialize previous count
prev_count = sys.maxsize
# We traverse bits from right to
# left and check if counts are
# decreasing order.
while (n > 0):
# Ignore 0s until we reach a
# set bit.
while (n > 0 and n % 2 == 0):
n = int(n/2)
# Count current set bits
curr_count = 1
while (n > 0 and n % 2 == 1):
n = n/2
curr_count += 1
# Compare current with previous
# and update previous.
if (curr_count >= prev_count):
return False
prev_count = curr_count
return True
# Driver code
n = 10
if (areSetBitsIncreasing(n)):
print("Yes")
else:
print("No")
# This code is contributed by Smitha
C#
// C# program to check if counts of
// consecutive 1s are increasing order.
using System;
class GFG {
// Returns true if n has counts of
// consecutive 1's are increasing
// order.
static bool areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = int.MaxValue;
// We traverse bits from right to
// left and check if counts are
// decreasing order.
while (n > 0)
{
// Ignore 0s until we reach
// a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;
// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}
// Compare current with previous
// and update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}
return true;
}
// Driver code
static public void Main ()
{
int n = 10;
if (areSetBitsIncreasing(n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by anuj_67.
PHP
0)
{
// Ignore 0s until we
// reach a set bit.
while ($n > 0 && $n % 2 == 0)
$n = $n / 2;
// Count current set bits
$curr_count = 1;
while ($n > 0 and $n % 2 == 1)
{
$n = $n / 2;
$curr_count++;
}
// Compare current with previous
// and update previous.
if ($curr_count >= $prev_count)
return false;
$prev_count = $curr_count;
}
return true;
}
// Driver code
$n = 10;
if (areSetBitsIncreasing($n))
echo "Yes";
else
echo "No";
// This code is contributed by anuj_67
?>
Javascript
输出 :
Yes
一种有效的解决方案是使用十进制到二进制的转换循环,该循环将数字除以2,然后将余数作为位。该循环从右到左查找位。因此,我们检查从右到左是否按降序排列。
下面是实现。
C++
// C++ program to check if counts of consecutive
// 1s are increasing order.
#include
using namespace std;
// Returns true if n has counts of consecutive
// 1's are increasing order.
bool areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = INT_MAX;
// We traverse bits from right to left
// and check if counts are decreasing
// order.
while (n > 0)
{
// Ignore 0s until we reach a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;
// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}
// Compare current with previous and
// update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}
return true;
}
// Driver code
int main()
{
int n = 10;
if (areSetBitsIncreasing(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program to check if counts of
// consecutive 1s are increasing order.
import java .io.*;
class GFG {
// Returns true if n has counts of
// consecutive 1's are increasing
// order.
static boolean areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = Integer.MAX_VALUE;
// We traverse bits from right to
// left and check if counts are
// decreasing order.
while (n > 0)
{
// Ignore 0s until we reach
// a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;
// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}
// Compare current with previous
// and update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}
return true;
}
// Driver code
static public void main (String[] args)
{
int n = 10;
if (areSetBitsIncreasing(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by anuj_67.
Python3
# Python3 program to check if counts of
# consecutive 1s are increasing order.
import sys
# Returns true if n has counts of
# consecutive 1's are increasing order.
def areSetBitsIncreasing(n):
# Initialize previous count
prev_count = sys.maxsize
# We traverse bits from right to
# left and check if counts are
# decreasing order.
while (n > 0):
# Ignore 0s until we reach a
# set bit.
while (n > 0 and n % 2 == 0):
n = int(n/2)
# Count current set bits
curr_count = 1
while (n > 0 and n % 2 == 1):
n = n/2
curr_count += 1
# Compare current with previous
# and update previous.
if (curr_count >= prev_count):
return False
prev_count = curr_count
return True
# Driver code
n = 10
if (areSetBitsIncreasing(n)):
print("Yes")
else:
print("No")
# This code is contributed by Smitha
C#
// C# program to check if counts of
// consecutive 1s are increasing order.
using System;
class GFG {
// Returns true if n has counts of
// consecutive 1's are increasing
// order.
static bool areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = int.MaxValue;
// We traverse bits from right to
// left and check if counts are
// decreasing order.
while (n > 0)
{
// Ignore 0s until we reach
// a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;
// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}
// Compare current with previous
// and update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}
return true;
}
// Driver code
static public void Main ()
{
int n = 10;
if (areSetBitsIncreasing(n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by anuj_67.
的PHP
0)
{
// Ignore 0s until we
// reach a set bit.
while ($n > 0 && $n % 2 == 0)
$n = $n / 2;
// Count current set bits
$curr_count = 1;
while ($n > 0 and $n % 2 == 1)
{
$n = $n / 2;
$curr_count++;
}
// Compare current with previous
// and update previous.
if ($curr_count >= $prev_count)
return false;
$prev_count = $curr_count;
}
return true;
}
// Driver code
$n = 10;
if (areSetBitsIncreasing($n))
echo "Yes";
else
echo "No";
// This code is contributed by anuj_67
?>
Java脚本
输出 :
No