c++ - C++ 中的阿兹特克金字塔

标签 c++

我是 C++ 新手。 我正在尝试创建一个程序,其输出如下所示

1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

这是我的尝试

#include <iostream>
using namespace std;
int const n=10;
int main (){
 int i,j,k,mat[n][n];   

    for(i=0;i<n;i++)
        for(j=0;j<n;j++){

            mat[i][j]=?
        }

    return 0;
}

最佳答案

解决方案效率低下,但似乎有效:

#include <iostream>

using namespace std;

int const n=10;

int main ()
{
    int i, j, mat[n][n] = { 0 };
    int indent;

    for (indent = 0; indent <= n/2; indent++) {
        for (i = indent; i < n-indent; i++) {
            for (j = indent; j < n-indent; j++) {
                mat[i][j]++;
            }
        }
    }

    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            cout << mat[i][j] << ' ';
        }
        cout << endl;
    }

    return 0;
}

关于c++ - C++ 中的阿兹特克金字塔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30334989/

相关文章:

c++ - 构造函数中的后期构造

c++ - 如何更改 Qt Creator 中的默认编译器标志

c++ - 带负数的递归最小值

c++ - 仅在 CComboBox 类型中包含某些项目?

使用自定义协议(protocol)时的 C++ 应用程序根目录

c++ - 可以在构造时修改命名的 std::string 吗?

c++ - 如何使用模板选择类(class)成员

c++ - 什么是对象切片?

c++ - 使用 POCO C++ 的非阻塞 WebSocket 服务器

python - 如何使用 boost.python 将预填充的 "unsigned char*"缓冲区传递给 C++ 方法?