それぞれ長さMのN個のパスワードを生成するC++プログラム
これは、それぞれ長さMのN個のパスワードを生成するC++プログラムです。
アルゴリズム
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. */ // Body of the function: 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 <<"Random Numbers are:" <<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 Random Numbers are: 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
-
乱数を生成するC++プログラム
C++を使用して乱数を生成する方法を見てみましょう。ここでは、0からある値の範囲のランダムな数値を生成しています。 (このプログラムでは、最大値は100です。) この操作を実行するために、srand()関数を使用しています。これはCライブラリにあります。関数voidsrand(unsigned int seed) 関数randで使用される乱数ジェネレーターをシードします 。 srand()の宣言は以下のようになります void srand(unsigned int seed) シードと呼ばれるパラメータを取ります。これは、疑似乱数ジェネレータアルゴリズムによってシードとして使用される整数
-
C++のCHAR_BIT
CHAR_BITは、charのビット数です。これは、C++言語の「limits.h」ヘッダーファイルで宣言されています。 1バイトあたり8ビットです。 これがC++言語のCHAR_BITの例です 例 #include <bits/stdc++.h> using namespace std; int main() { int x = 28; int a = CHAR_BIT*sizeof(x); stack<bool> s; cout << "T