Matrix of Differences(构造)
题目描述:
For a square matrix of integers of size
For example, for the matrix
You are given an integer
输入描述:
The first line contains a single integer
The first (and only) line of each test case contains a single integer
输出描述:
For each test case, print
样例:
input:
2
2
3
output:
1 3
4 2
1 3 4
9 2 7
5 8 6
AC代码:
#include <bits/stdc++.h> using namespace std; int n; void solve() { cin >> n; // 相当与一个n * n的二维数组,可以用a[][]来表示,但n不宜太大 vector<vector<int>> a(n, vector<int> (n)); int l = 1, r = n * n, t = 0; // 蛇形排列 // 第一行从左到右,第二行从右到左,第三行从左到右...依此类推 for(int i = 0; i < n; i ++) { for(int j = 0; j < n; j ++) { if(t) { a[i][j] = l ++; } else { a[i][j] = r --; } t ^= 1; } if(i & 1) { reverse(a[i].begin(), a[i].end()); } } for(int i = 0; i < n; i ++) { for(int j = 0; j < n; j ++) { // 相当于一个字符串a = " \n"; // 输出a[0] 或者 a[1]; cout << a[i][j] << " \n"[j == n - 1]; } } } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T; cin >> T; while(T --) solve(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】