题解 | #蛇形矩阵#

蛇形矩阵

https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e

看出规律就做的
#include <iostream>
using namespace std;

int main(){
    int n;
    cin >> n;
    int col = n, start_x = 1, d_x, num;
    int start_d_x = 2, start_d_y = 1;
    for (int i = 0; i < n; i++) {
        num = start_x;
        d_x = start_d_x;
        for (int j = 0; j < col; j++) {
            cout << num << " ";
            num = num + d_x;
            d_x = d_x + 1;
        }
        cout << endl;
        start_d_x++;
        start_x = start_x + start_d_y;
        start_d_y++;
        col--;
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务