题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
#include <stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int num[n]; num[0] = 1; //将第一排赋值给数组,公式为A[n] = A[n-1] + n for(int i = 1; i < n; i++){ num[i] = num[i-1] + i + 1; } //打印第一排 for (int i = 0; i < n; i++) { printf("%d ", num[i]); } printf("\n"); //打印后续矩阵,公式为右上角的数字-1 for (int i = 1; i < n; i++) { for (int j = 0; j < n - i; j++) { printf("%d ", num[j+1] - 1); num[j] = num[j+1] - 1; //更新数组 } printf("\n"); } } }