洛谷 P5731 蛇形方阵 题解

本文介绍了洛谷P5731题目关于蛇形方阵的解题思路,通过定义二维数组并设置方向判断,依次填充数值。详细讲解了从左到右、从上到下、从右到左、从下到上的赋值过程,并提供了C++代码实现,特别提到了setw在C++中用于控制输出宽度的功能。
摘要由CSDN通过智能技术生成
这玩意儿是红题?

像这种题倒不如打表:

#include<bits/stdc++.h>
using namespace std;

int main(){
   
	int n;
	cin>>n;
	if(n==1){
   
		cout<<"  1";
	} 
	if(n==2){
   
		cout<<"  1  2"<<endl;
		cout<<"  4  3"<<endl;
	}
	if(n==3){
   
		cout<<"  1  2  3"<<endl;
		cout<<"  8  9  4"<<endl;
		cout<<"  7  6  5"<<endl;
	}
	if(n==4){
   
		cout<<"  1  2  3  4"<<endl;
		cout<<" 12 13 14  5"<<endl;
		cout<<" 11 16 15  6"<<endl;
		cout<<" 10  9  8  7"<<endl;
	}
	if(n==5){
   
		cout<<"  1  2  3  4  5"<<endl;
		cout<<" 16 17 18 19  6"<<endl;
		cout<<" 15 24 25 20  7"<<endl;
		cout<<" 14 23 22 21  8"<<endl;
		cout<<" 13 12 11 10  9"<<endl;
	}
	if(n==6){
   
		cout<<"  1  2  3  4  5  6"<<endl;
		cout<<" 20 21 22 23 24  7"<<endl;
		cout<<" 19 32 33 34 25  8"<<endl;
		cout<<" 18 31 36 35 26  9"<<endl;
		cout<<" 17 30 29 28 27 10"<<endl;
		cout<<" 16 15 14 13 12 11"<<endl;
	}
	if(n==7){
   
		cout<<"  1  2  3  4  5  6  7"<<endl;
		cout<<" 24 25 26 27 28 29  8"<<endl;
		cout<<" 23 40 41 42 43 30  9"<<endl;
		cout<<" 22 39 48 49 44 31 10"<<endl;
		cout<<" 21 38 47 46 45 32 11"<<endl;
		cout<<" 20 37 36 35 34 33 12"<<endl;
		cout<<" 19 18 17 16 15 14 13"<<endl;
	}
	if(n==8)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值