# include # include bool open ( int n, int p[] ); void perm_next ( int n, int p[], int &rank ); using namespace std; int main ( ) // // BUTTON_PERM searches for the combination to a button lock by checking permutations. // { bool found; int i, n = 10, p[10], rank; found = false; rank = -1; while ( true ) { perm_next ( n, p, rank ); if ( rank == -1 ) { break; } // // If this combination works, we're done! // if ( open ( n, p ) ) { found = true; cout << " Found combination: "; for ( i = 0; i < n; i++ ) { cout << " " << p[i]; } cout << "\n"; break; } } if ( !found ) { cout << "We did not find the combination.\n"; } return 0; } bool open ( int n, int p[] ) { bool value; if ( p[0] == 1 && p[1] == 4 && p[2] == 3 && p[3] == 6 && p[4] == 0 && p[5] == 9 && p[6] == 5 && p[7] == 8 && p[8] == 2 && p[9] == 7 ) { value = true; } else { value = false; } return value; }