Problem 2491 >> 问题 P: 蛇形方阵(三星宗师) |
luoen @ 2024-12-27 18:37:57
怎么写啊,各位大神
|
luoen @ 2024-12-27 18:38:07
题目描述
给出一个不大于 9 的正整数 n,输出n*n的蛇形方阵。 这类方阵也称为回形矩阵。 从左上角填上 1 开始,顺时针方向依次填入数字,如同样例所示。注意每个数字有都会占用 3 个字符,前面使用空格补齐。 输入格式 一个正整数n 输出格式 输出n*n的蛇形方程 输入样例 复制 4 输出样例 复制 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 分类标签 ★★★★ |
yuzihao @ 2025-01-04 12:30:20
#include <iostream>
#include <iomanip> using namespace std; int main() { int n; cin >> n; int arr[n][n]; int num = 1; int top = 0, bottom = n - 1, left = 0, right = n - 1; while (top <= bottom && left <= right) { // 向右填充 for (int i = left; i <= right; i++) { arr[top][i] = num++; } top++; // 向下填充 for (int i = top; i <= bottom; i++) { arr[i][right] = num++; } right--; // 向左填充 if (top <= bottom) { for (int i = right; i >= left; i--) { arr[bottom][i] = num++; } bottom--; } // 向上填充 if (left <= right) { for (int i = bottom; i >= top; i--) { arr[i][left] = num++; } left++; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << setw(3) << arr[i][j]; } cout << endl; } return 0; } |
yuzihao @ 2025-01-04 12:30:32
C++代码
|
luoen @ 2025-01-05 10:23:45
灰常感谢
|
pengyufan @ 2025-01-07 17:27:52
o1k82
|
pengyufan @ 2025-01-07 17:42:45
1d09y
|
pengyufan @ 2025-01-07 17:46:49
e4l1y
|
pengyufan @ 2025-01-07 17:51:21
#include<bits/stdc++.h>
using namespace std; int main(){ int n; cin>>n; int a[n+5]; int in=0; for(int i=0;i<n;i++){ int x; cin>>x; if(x%2!=0){ a[in]=x; in++; } } for(int i=0;i<in-1;i++){ for(int j=0;j<in-i-1;j++){ if(a[j]>a[j+1]){ int t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for(int i=0;i<in;i++){ if(i==in-1){ cout<<a[i]; }else{ cout<<a[i]<<','; } } return 0; } |
lanyubo @ 2025-01-07 17:59:45
#include<bits/stdc++.h>
using namespace std; int main(){ int n; cin>>n; int a[n+1]; for(int i=1;i<=n;i++){ cin>>a[i]; } stack<int >s; int p=1; for(int i=1;i<=n;i++){ for(int j=p;j<=a[i];j++){ s.push(p); p++; } if(s.top()==a[i]){ s.pop(); }else{ cout<<"NO"; return 0; } } cout<<"YES"; return 0; } |
zhangwenbo @ 2025-02-26 21:28:10
666居然是保存代码导致讨论版被删大蛇,畏惧了
|