AS YOU HAVE SEEN MANY PROBLEMS IN VARIOUS CODING COMPETITION LIKE TOPCODER , CODECHEF , CODEFORCE FOR OUTPUT THE NUMBER OF OCCURRENCES OF THE VARIOUS ITEMS TOGETHER OR DIFFERENTLY. AS IN THIS CODE I HAVE TAKEN TWO ITEMS C AND D AND THIS PROGRAMME WILL OUTPUT THE COMBINATIONS OF THESE TWO IN 5 PLACES. THIS IS A SIMPLE CODE WRITTEN BY ME.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int numLength = 5;
string tmp ;
int x = 0;
for(int k = 0 ; k <(1<<numLength); k++) // it forms the combinations
{
x = k;
for(int i = 0 ; i <numLength ; i++)
{
if( (x & 1) == 1)
tmp+= 'c';
else
tmp+= 'd';
x>>= 1; // as it right shift the value so the combination occurs
}
cout<<k<<'\t'<<tmp<<endl;
tmp = "";
}
cout<<tmp<<endl;
x = 5;
cout<<(x>>1)<<endl;
cout<<(x&1)<<endl;
cin.get();
return 0;
}
BY : SAURABH BHATIA
#include<iostream>
#include<string>
using namespace std;
int main()
{
int numLength = 5;
string tmp ;
int x = 0;
for(int k = 0 ; k <(1<<numLength); k++) // it forms the combinations
{
x = k;
for(int i = 0 ; i <numLength ; i++)
{
if( (x & 1) == 1)
tmp+= 'c';
else
tmp+= 'd';
x>>= 1; // as it right shift the value so the combination occurs
}
cout<<k<<'\t'<<tmp<<endl;
tmp = "";
}
cout<<tmp<<endl;
x = 5;
cout<<(x>>1)<<endl;
cout<<(x&1)<<endl;
cin.get();
return 0;
}
BY : SAURABH BHATIA
No comments:
Post a Comment