A number is called Well Order if every digit is greater than previous one(Eg: 238 is well Order as 2<3<8 but 132 is not). Given length of a number, Find all well ordered numbers of Given Length
Input: Length of number
Output : Well Ordered numbers of given length
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int p;
void PrintNum ( int prefix,int s, int n )
{
if ( n==0 && prefix>=pow ( 10.0, p-1 ) )
{
cout<< prefix<<"\n";
}
else
{
for ( int i=s; i<10; i++ )
{
PrintNum ( 10*prefix+i, i+1, n-1 );
}
}
}
int main()
{
cin>>p;
PrintNum ( 0,0,p );
getchar();
}
Please comment if you find anything incorrect or have any inputs to improve the code.
Input: Length of number
Output : Well Ordered numbers of given length
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int p;
void PrintNum ( int prefix,int s, int n )
{
if ( n==0 && prefix>=pow ( 10.0, p-1 ) )
{
cout<< prefix<<"\n";
}
else
{
for ( int i=s; i<10; i++ )
{
PrintNum ( 10*prefix+i, i+1, n-1 );
}
}
}
int main()
{
cin>>p;
PrintNum ( 0,0,p );
getchar();
}
Please comment if you find anything incorrect or have any inputs to improve the code.
No comments:
Post a Comment