K11703 螺旋加密c++代码
时间: 2023-07-09 18:16:08 浏览: 63
以下是 K11703 螺旋加密的 C++ 代码实现:
```c++
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
/**
* 将明文填充到正方形矩阵中
* @param plaintext 明文字符串
* @param matrix 正方形矩阵
*/
void fillMatrix(const string& plaintext, char** matrix, int n) {
int row = 0, col = 0, index = 0;
while (index < plaintext.length()) {
// 向右填充
while (col < n && matrix[row][col] == '\0') {
matrix[row][col++] = plaintext[index++];
}
col--; row++;
// 向下填充
while (row < n && matrix[row][col] == '\0') {
matrix[row++][col] = plaintext[index++];
}
row--; col--;
// 向左填充
while (col >= 0 && matrix[row][col] == '\0') {
matrix[row][col--] = plaintext[index++];
}
col++; row--;
// 向上填充
while (row >= 0 && matrix[row][col] == '\0') {
matrix[row--][col] = plaintext[index++];
}
row++; col++;
}
}
/**
* 将矩阵按照螺旋路径遍历,得到密文
* @param matrix 正方形矩阵
* @return 密文字符串
*/
string spiralTraversal(char** matrix, int n) {
string ciphertext;
int row = 0, col = 0, index = 0;
while (index < n * n) {
// 向右遍历
while (col < n && matrix[row][col] != '\0') {
ciphertext += matrix[row][col++];
index++;
}
col--; row++;
// 向下遍历
while (row < n && matrix[row][col] != '\0') {
ciphertext += matrix[row++][col];
index++;
}
row--; col--;
// 向左遍历
while (col >= 0 && matrix[row][col] != '\0') {
ciphertext += matrix[row][col--];
index++;
}
col++; row--;
// 向上遍历
while (row >= 0 && matrix[row][col] != '\0') {
ciphertext += matrix[row--][col];
index++;
}
row++; col++;
}
return ciphertext;
}
/**
* K11703 螺旋加密
* @param plaintext 明文字符串
* @param n 矩阵的边长
* @return 密文字符串
*/
string k11703SpiralEncryption(const string& plaintext, int n) {
char** matrix = new char*[n];
for (int i = 0; i < n; i++) {
matrix[i] = new char[n];
for (int j = 0; j < n; j++) {
matrix[i][j] = '\0';
}
}
fillMatrix(plaintext, matrix, n);
string ciphertext = spiralTraversal(matrix, n);
for (int i = 0; i < n; i++) {
delete[] matrix[i];
}
delete[] matrix;
return ciphertext;
}
int main() {
string plaintext = "hello world";
int n = ceil(sqrt(plaintext.length()));
string ciphertext = k11703SpiralEncryption(plaintext, n);
cout << "Plaintext: " << plaintext << endl;
cout << "Ciphertext: " << ciphertext << endl;
return 0;
}
```
注意,该代码仅为示例,实际使用时需要考虑更多的异常情况和安全性问题。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)