Here's my Implementation i take the number and put it into the stack by doing mathematical calculation ... but it's not producing correct output even my logic regarding the problem is correct ... Please detect what is wrong in my implementation. It's competitive programming Question asked by two companies in my College . So, please help me to correct the implementation.... I only want to know what is wrong with my implementation .....
Actual Question: Input: The first line of input contains an integer T denoting the number of test cases. There will be a single line for each testcase which contains N. Output: Print all binary numbers with decimal values from 1 to N in a single line. Constraints: 1 ≤ T ≤ 106 1 ≤ N ≤ 106
Example: Input: 2 2 5
Output: 1 10 1 10 11 100 101
Explanation: Testcase 1: Binary numbers from 1 to 2 are 1 and 10.
This is the Required Output Input: 2 2 5
Output: 1 10 1 10 11 100 101
And my output is : For Input: 1 3 Your Output is:
Can any one here tell me the reason of incorrect output ..
#include<iostream>
#include <stack>
#include <queue>
using namespace std;
int main()
{
int T ;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
stack <int > s;
int num=i;
while(num>0)
{
s.push(num%2);
num=num/2 ;
}
for(int j=0;j<s.size();j++)
{
cout<<s.top();
s.pop();
}
cout<<" ";
}
cout<<endl;
}
return 0
This is the Required Output
Input:
2
2
5
Output:
1 10
1 10 11 100 101
And my output is :
For Input:
1
3
Your Output is:
Can any one here tell me the reason of incorrect output ..