要随机算法的代码C++的
时间: 2023-06-27 11:02:24 浏览: 43
以下是一个简单的随机算法的 C++ 代码示例:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
// 使用当前时间作为随机数种子
srand(time(0));
// 生成随机数
int randomNumber = rand();
// 输出随机数
cout << "随机数为:" << randomNumber << endl;
return 0;
}
```
该程序使用了 `srand()` 函数来设置随机数种子,使用 `rand()` 函数生成随机数。需要注意的是,`srand()` 函数只需要在程序中调用一次即可,通常使用当前时间作为种子,以保证每次运行程序时生成的随机数不同。
相关问题
无序整数序列找第k小 随机算法代码C++
以下是基于快排思想的随机算法,可以找到一个无序整数序列中第k小的数:
```c++
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int partition(int arr[], int l, int r) {
int pivot = arr[r];
int i = l - 1;
for(int j = l; j < r; j++) {
if(arr[j] <= pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i+1], arr[r]);
return i+1;
}
int randomPartition(int arr[], int l, int r) {
int n = r-l+1;
int pivot = rand() % n;
swap(arr[l+pivot], arr[r]);
return partition(arr, l, r);
}
int kthSmallest(int arr[], int l, int r, int k) {
if(k > 0 && k <= r-l+1) {
int pos = randomPartition(arr, l, r);
if(pos-l == k-1)
return arr[pos];
if(pos-l > k-1)
return kthSmallest(arr, l, pos-1, k);
return kthSmallest(arr, pos+1, r, k-pos+l-1);
}
return INT_MAX;
}
int main() {
int n, k;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for(int i = 0; i < n; i++)
cin >> arr[i];
cout << "Enter the value of k: ";
cin >> k;
srand(time(NULL));
cout << "The " << k << "th smallest element is " << kthSmallest(arr, 0, n-1, k);
return 0;
}
```
注:该算法的时间复杂度为O(n),其中n为序列长度。
rsa加密算法代码c++
以下是C++实现的RSA加密算法代码,其中包括密钥生成,加密和解密功能。
```c++
#include <iostream>
#include <cmath>
using namespace std;
// 求最大公约数
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// 求扩展欧几里得算法的 x 值
int extended_gcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int gcd = extended_gcd(b, a % b, x, y);
int tmp = x;
x = y;
y = tmp - a / b * y;
return gcd;
}
// 计算 a^b mod n
int fast_power_mod(int a, int b, int n)
{
int ans = 1;
while (b)
{
if (b & 1)
ans = (ans * a) % n;
a = (a * a) % n;
b >>= 1;
}
return ans;
}
// 生成 RSA 密钥
void generate_rsa_key(int p, int q, int &n, int &e, int &d)
{
// 计算 n = p * q
n = p * q;
// 计算φ(n) = (p - 1) * (q - 1)
int phi_n = (p - 1) * (q - 1);
// 随机选择一个整数 e,满足 1 < e < φ(n) 且 gcd(e, φ(n)) = 1
e = 65537; //常用值,可以加快加密解密速度
while (gcd(e, phi_n) != 1)
e++;
// 计算 d,使得 e * d ≡ 1 (mod φ(n))
int x, y;
extended_gcd(e, phi_n, x, y);
d = (x % phi_n + phi_n) % phi_n;
}
// RSA 加密
int rsa_encrypt(int msg, int e, int n)
{
return fast_power_mod(msg, e, n);
}
// RSA 解密
int rsa_decrypt(int cipher, int d, int n)
{
return fast_power_mod(cipher, d, n);
}
int main()
{
int p = 61;
int q = 53;
int n, e, d;
generate_rsa_key(p, q, n, e, d);
int msg = 12345;
int cipher = rsa_encrypt(msg, e, n);
int decrypted_msg = rsa_decrypt(cipher, d, n);
cout << "p = " << p << endl;
cout << "q = " << q << endl;
cout << "n = " << n << endl;
cout << "e = " << e << endl;
cout << "d = " << d << endl;
cout << "Message: " << msg << endl;
cout << "Encrypted Message: " << cipher << endl;
cout << "Decrypted Message: " << decrypted_msg << endl;
return 0;
}
```
需要注意的是,RSA加密算法是一种非常安全的加密算法,但是在实际应用中,需要注意密钥的保护和管理,避免密钥被泄露。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)