螺旋队列C++代码实现实例及运行结果

实现螺旋队列,并且输入任意坐标时输出对应的值

c++代码

#include <iostream>
#include<iomanip>
#define max(a,b) ((a>b)?a:b)
#define abs(a) ((a>0)?a:(-a))

using namespace std;

int fun(int x,int y)
{
    int t=max(abs(x),abs(y));
    int u=t+t;
    int v=u-1;
    v=v*v+u;
    if(x==-t)
        v+=u+t-y;
    else if(y==-t)
        v+=3*u+x-t;
    else if(y==t)
        v+=t-x;
    else
        v+=y-t;
    return v;
}

int main()
{
    int x,y;//x,y为坐标
    for(y=-4;y<=4;y++)
    {
        for(x=-4;x<=4;x++)
            cout<<setw(3)<<fun(x,y);//setw(n)设置n个域宽
        cout<<endl;
    }

    while(1)
    {
        cout<<endl<<"请输入x,y的值,输入10000时结束程序"<<endl;
        cin>>x;
        if(x==10000)
            break;
        cin>>y;
        cout<<fun(x,y);
    }

    return 0;
}


运行结果



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 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; } ``` 注意,该代码仅为示例,实际使用时需要考虑更多的异常情况和安全性问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值