-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_all_possible_password.txt
51 lines (47 loc) · 1.36 KB
/
generate_all_possible_password.txt
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Find all the possible passwords, given the length of the password and that it is a well ordered number (159 is well-ordered as 1<5<9)
*/
#include <iostream>
using namespace std;
/*outputPW() print all the possible combination of password, given the length of the password.*/
void outputPW(int arr[], int len, int index){
if (index==len){
/*decide if the password is well-ordered, that is strictly ascending. If so, print it; otherwise discare it.*/
int flag=1;
for (int i=0; i<len-1; ++i){
if (arr[i]>=arr[i+1]){
flag=0;
break;
}
}
if (flag==1){
for (int i=0; i<len; ++i)
cout<<arr[i];
cout<<endl;
return;
}
else return;
}
else{
for (int i=0; i<=9; ++i){
arr[index]=i;
outputPW(arr,len,index+1);
}
}
}
int main(){
cout<<"Input the length of the password:"<<endl;
int length;
cin>>length;
if (length<0){
cout<<"The length cannot be negative."<<endl;
return -1;
}
int *arr = new int[length];
outputPW(arr,length, 0);
delete arr;
return 0;
}
REMARK
1. First self written recursive code. Similar to the other code "keypad" in the repository "Hello-World".
2. COMMON ERROR, wrote "if (flag==0)" into "if (flag=0)".