Generate Binary Numbers

PROBLEM :
Given a number n, Write a program that generates and prints all binary numbers with decimal values from 1 to n.

Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.

Output:
Print all binary numbers with decimal values from 1 to n in a single line.

Constraints:
1 = T = 100
1 = N = 500

Example:
Input
2
2
5

Output
1 10
1 10 11 100 101

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<queue>
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
   cin>>no ;
   queue<int> q ;
   q.push(1) ;
 
   while(no--)
   {
       int curr=q.front() ;
       q.pop() ;
     
       cout<<curr<<" " ;
       int Curr=curr ;
       curr=curr*10 ;
       q.push(curr) ;
       Curr=Curr*10+1 ;
       q.push(Curr) ;
   }
   cout<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )