Hi guys, I'm trying to solve this interview question:
"Write a function which, given n, prints all well-ordered integers of n digits. A well ordered number is one where the value of the i-th digit is less than the value of the i+1 digit."
I've written the code where if n=3. Then I go from 100 to 999. And check if it is well-ordered. But my approach doesn't take care of numbers like 012, 018 etc...Numbers which start with a 0.
Please tell me how to do it.
Here is my current code:
#include<iostream>
#include<cmath>
using namespace std;
bool desirable(int num, int N)
{
int digits[N];
for (int i=0; i<N; i++)
{
digits[i]=num%10;
num/=10;
}
for (int i=0;i<N-1;i++)
{
if (digits[i]>digits[i+1])
continue;
else
return false;
}
return true;
}
int main()
{
printf("Enter: ");
int N;
scanf("%d",&N);
int low = (int)pow(10.0, double(N-1));
int high = (int)pow(10.0, double(N)) -1;
for (int i=low; i<=high; i++)
{
if(desirable(i,N))
printf("%d\n",i);
}
return 0;
}