给定一个限制N,我们需要找出小于N的二进制数字的数量。二进制数字是那些只包含0和1的数字,因为它们的数字为1、10、101等都是二进制数字。
例子:
Input : N = 200
Output : 7
Count of binary digit number smaller than N is 7,
enumerated below,
1, 10, 11, 110, 101, 100, 111
解决此问题的一种简单方法是从1循环到N,并检查每个数字是否为二进制数字。如果它是一个二进制数字,请增加此类数字的计数,但是此过程将花费O(N)时间。我们可以做得更好,因为我们知道此类数字的数量将远小于N,因此我们只能对二进制数字进行迭代,并检查生成的数字是否小于N。
在下面的代码中,类似于BFS的方法被实现为仅对二进制数字进行迭代。我们从1开始,每次将(t * 10)和(t * 10 +1)推入队列,其中t是弹出的元素,如果t是二进制数字,则(t * 10)和(t * 10 +1)也会是二进制数字,因此我们仅使用队列来遍历这些数字。当弹出的数字越过N时,我们将停止在队列中推送元素。
C++
// C++ program to count all binary digit
// numbers smaller than N
#include
using namespace std;
// method returns count of binary digit
// numbers smaller than N
int countOfBinaryNumberLessThanN(int N)
{
// queue to store all intermediate binary
// digit numbers
queue q;
// binary digits start with 1
q.push(1);
int cnt = 0;
int t;
// loop untill we have element in queue
while (!q.empty())
{
t = q.front();
q.pop();
// push next binary digit numbers only if
// current popped element is N
if (t <= N)
{
cnt++;
// uncomment below line to print actual
// number in sorted order
// cout << t << " ";
q.push(t * 10);
q.push(t * 10 + 1);
}
}
return cnt;
}
// Driver code to test above methods
int main()
{
int N = 200;
cout << countOfBinaryNumberLessThanN(N);
return 0;
}
Java
import java.util.LinkedList;
import java.util.Queue;
// java program to count all binary digit
// numbers smaller than N
public class GFG {
// method returns count of binary digit
// numbers smaller than N
static int countOfBinaryNumberLessThanN(int N) {
// queue to store all intermediate binary
// digit numbers
Queue q = new LinkedList<>();
// binary digits start with 1
q.add(1);
int cnt = 0;
int t;
// loop untill we have element in queue
while (q.size() > 0) {
t = q.peek();
q.remove();
// push next binary digit numbers only if
// current popped element is N
if (t <= N) {
cnt++;
// uncomment below line to print actual
// number in sorted order
// cout << t << " ";
q.add(t * 10);
q.add(t * 10 + 1);
}
}
return cnt;
}
// Driver code to test above methods
static public void main(String[] args) {
int N = 200;
System.out.println(countOfBinaryNumberLessThanN(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to count all binary digit
# numbers smaller than N
from collections import deque
# method returns count of binary digit
# numbers smaller than N
def countOfBinaryNumberLessThanN(N):
# queue to store all intermediate binary
# digit numbers
q = deque()
# binary digits start with 1
q.append(1)
cnt = 0
# loop untill we have element in queue
while (q):
t = q.popleft()
# push next binary digit numbers only if
# current popped element is N
if (t <= N):
cnt = cnt + 1
# uncomment below line to print actual
# number in sorted order
q.append(t * 10)
q.append(t * 10 + 1)
return cnt
# Driver code to test above methods
if __name__=='__main__':
N = 200
print(countOfBinaryNumberLessThanN(N))
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to count all binary digit
// numbers smaller than N
using System;
using System.Collections.Generic;
class GFG
{
// method returns count of binary digit
// numbers smaller than N
static int countOfBinaryNumberLessThanN(int N)
{
// queue to store all intermediate binary
// digit numbers
Queue q = new Queue();
// binary digits start with 1
q.Enqueue(1);
int cnt = 0;
int t;
// loop untill we have element in queue
while (q.Count > 0)
{
t = q.Peek();
q.Dequeue();
// push next binary digit numbers only if
// current popped element is N
if (t <= N)
{
cnt++;
// uncomment below line to print actual
// number in sorted order
q.Enqueue(t * 10);
q.Enqueue(t * 10 + 1);
}
}
return cnt;
}
// Driver code
static void Main()
{
int N = 200;
Console.WriteLine(countOfBinaryNumberLessThanN(N));
}
}
// This code is contributed by mits
PHP
输出:
7