Need Help with Programming Problem

I have thought about this problem for extremely long, and absolutely cannot think of a solution. Question follows.

Write a program that prints all the 3-digit numbers that are composed of {0,1,2,3,4,5} and whose digits are distinct.

Additional conditions are that the solution must not utilize nested loops.
Last edited on
There are only 20 ways to choose 3 numbers from 6 and only 6 ways to permute those 3 numbers.

So you could define an array with all the 3 number combos, like:

1
2
3
4
5
6
7
8
    int a[20][3] = {
        {0,1,2},
        {0,1,3},
        {0,1,4},
        {0,1,5},
        {0,2,3},
        etc...
    };

Just loop through that array and send the 3 values to a print function that prints all the permutations:

1
2
3
4
5
void print(int a, int b, int c) {
    std::cout << a << b << c << '\n';
    std::cout << a << c << b << '\n';
    etc...
}

Last edited on
Thanks for the reply,
Is there a way to do this without utilizing an array? We are not supposed to have learnt it yet.
Brute force (recursive):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

// print the number if it is a three digit number with unique digits
void print_if_unique_three_digit_number( unsigned int number )
{
    // determine the three digits
    const unsigned int a = number/100 ;
    const unsigned int b = (number/10) % 10 ;
    const unsigned int c = number%10 ;

    // if first digit is non-zero and the three digits are distinct, print it
    if( a != 0 && a != b && b != c && c != a ) std::cout << number << '\n' ;
}

// print all three digit numbers with unique digits formed by the digits [ 0, max_digit ]
void print_all_unique_three_digit_numbers( unsigned int max_digit = 5, unsigned int num_digits = 0, unsigned int number_so_far = 0 )
{
    // we have a number formed from the requisite number of digits, print it if it qualifies
    if( num_digits == 3 ) print_if_unique_three_digit_number(number_so_far) ;

    else // less than three digits, append one more digit and try again
    {
        for( unsigned int i = 0; i <= max_digit ; ++i ) // for each possible digit
            print_all_unique_three_digit_numbers( max_digit, num_digits+1, number_so_far*10 + i ) ;
    }
}

int main()
{
    print_all_unique_three_digit_numbers(5) ; // digits chosen from [ 0, 1, 2, 3, 4, 5 ]
}

http://coliru.stacked-crooked.com/a/664f71442506e88b
closed account (3U5X216C)
@afatperson first of all understand that there will be 5*5*4 = 100 combinations of numbers.
also see my code hope it helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


#include <bits/stdc++.h>
using namespace std;

int main() {
    string str = "012345";
    set<string> s;
    do {
        string ss = "";
        for (int i = 0; i < 3; ++i) {
            ss = ss + str[i];
        }
        if (ss[0] != '0') {
            s.insert(ss);
        }
    } while (next_permutation(str.begin(), str.end()));
    cout << s.size();
    for (auto i : s) {
        cout << i << endl;
    }
    return EXIT_SUCCESS;
}
closed account (3U5X216C)
@afatperson sorry i didn't read your question properly do dont want nesting of loops so you can also try using this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <bits/stdc++.h>
using namespace std;

int main() {
    string str = "012345";
    string ss = "";
    set<string> s;
    do {
        if (str[0] != '0') {
            s.insert(ss+str[0]+str[1]+str[2]);
        }
    } while (next_permutation(str.begin(), str.end()));
    cout << s.size();
    for (auto i : s) {
        cout << i << endl;
    }
    return EXIT_SUCCESS;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <set>
using namespace std;

string toBase( int n, int base )
{
   return n == 0 ? "" : toBase( n / base, base ) + to_string( n % base );
}

bool noRepeats( string s )
{
   set<char> S( s.begin(), s.end() );
   return S.size() == s.size();
}

int main()
{
   int start = 38, end = 215;
   for ( int n = start; n <= end; n++ )
   {
      string s = toBase( n, 6 );
      if ( noRepeats( s ) ) cout << s << '\n';
   }
}
Last edited on
direct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main() 
{
    // 3-digit numbers with unique digits from {0,1,2,3,4,5}
    int a,b,c;
    for (int n=102; n<=543; ++n)
    {
        a = n/100;
        b = n/10 % 10;
        c = n%10;
        if (a>5 || b>5 || c>5)
            continue;
            
        if (a!=b && b!=c && a!=c)
            cout << n << '\n';
    }
    return 0;
}
Last edited on
That's really neat, icy1. Simplicity is the ultimate sophistication, and all that.
Inspired by icy1's:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main() {
    // Interpretting n as base 6
    for (int n = 0; n < 216; n++) {
        int a = n / 36;
        int b = n / 6 % 6;
        int c = n % 6;
        if (a!=b && a!=c && b!=c)
            std::cout << a << b << c << '\n';
    }
}

icy1, exactly what I was looking for, we are not supposed to have learnt anything past logical operators or do-while, while, and for loops.
Topic archived. No new replies allowed.