Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- using namespace std;
- void printNumList(int level);
- void printNumber(int index, char *seq, const int maxLevel);
- int main()
- {
- while (1){
- printf("Press 0 to exit.\n");
- printf("Press 1 to do the nonrecursive mission.\n");
- printf("Press 2 to do the recursive mission.\n");
- int Num;
- scanf("%d", &Num);
- if (Num == 0) { //exit
- break;
- }
- else if (Num == 1){ //nonrecursive
- }
- else if (Num == 2){ //recursive
- printf("Please enter a number between 1 to 9: ");
- int InputNum;
- cin >> InputNum;
- if(InputNum > 9 || InputNum < 1) {
- printf("Invalid number, please try again.\n");
- }
- // Do the process
- printNumList(InputNum);
- }
- }
- return 0;
- }
- void printNumList(int level)
- {
- // create output string
- char *seq = new char[level+1];
- seq[level] = '\0';
- //start recursion
- printNumber(0, seq, level-1);
- }
- void printNumber(int index, char *seq, const int maxLevel)
- {
- // recursion stop point
- if (index > maxLevel)
- return;
- for (char c = '0'; c <= '9'; ++c){
- // check repeat
- bool repeat = false;
- for (int j = 0; seq[j] != '\0'; ++j){
- if ( c == seq[j]){
- repeat = true;
- break;
- }
- }
- if (repeat)
- continue;
- // assign number
- seq[index] = c;
- printNumber(index+1, seq, maxLevel);
- // output
- if (index == maxLevel){
- cout << seq << endl;
- }
- // clear number in seqence
- seq[index] = '\0';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement