给定输出的逻辑是什么

what would be the logic for the given output

本文关键字:是什么 输出      更新时间:2023-10-16

这是一个模式程序的输出。我发现很难找到以下输出的逻辑。请用C++编写代码...

输出:

12 93 8 104 7 11 145 6 12 13 15
#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int arr[n+1][n+1];
    int st, ed, inc;
    int num = 1;
    for(int j = 1; j <= n; j++) {
        if(j%2==1) {
            st = j, ed = n+1, inc = 1;
        } else {
            st = n, ed = j-1, inc = -1;
        }
        for(int i = st; i != ed; i += inc) {
            arr[i][j] = num;
            num++;
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= i; j++) {
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

输入: 5

输出:

12 9
3 8 10
4 7 11 14
5 6 12 13 15