C ++程序生成N个密码,每个密码的长度为M
这是一个C++程序,用于生成N个密码,每个密码的长度为M。
算法
Begin
Take the length of password as input.
function permutation() generate random passwords:
/* Arguments
A pointer array a.
Total Number of random numbers m.
Length of the password s.
*/
//函数主体:
if (m == s)
for i = 0 to s-1
Print *(a + i)
else
for i = m to s-1
int tmp = a[m]
a[m] = a[i]
a[i] = tmp
Call permutation(a, m + 1, s)
tmp = a[m]
a[m] = a[i]
a[i] = tmp
End示例
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void permutation(int *a, int m, int s) {
if (m == s) {
for (int i = 0; i < s; i++) {
cout << *(a + i);
}
cout << endl;
} else {
for (int i = m; i < s; i++) {
int tmp = a[m];
a[m] = a[i];
a[i] = tmp;
permutation(a, m + 1, s);
tmp = a[m];
a[m] = a[i];
a[i] = tmp;
}
}
}
int main(int argc, char **argv) {
cout << "Enter the length of the password: ";
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
a[i] = rand() % 10; //randomly generate numbers
}
cout <<"随机数是:" <<endl;
for (int i = 0; i < n; i++) {
cout<<a[i] <<endl;
}
cout << "The Passwords are: "<<endl;
permutation(a, 0, n);
}输出结果
Enter the length of the password: 4 随机数是: 1740T he Passwords are: 1740 1704 1470 1407 1047 1074 7140 7104 7410 7401 7041 7014 4710 4701 4170 4107 4017 4071 0741 0714 0471 0417 0147 0174
热门推荐
10 教练生日祝福语 简短独特
11 许嵩生日祝福语简短
12 新娘上头祝福语大全简短
13 特别的端午祝福语简短
14 老师祝福语简短100字
15 农民朋友发财祝福语简短
16 万寿祝福语简短
17 爱情贺卡祝福语简短英语
18 古诗生日祝福语简短独特