#include<stdio.h>
#include <string.h>
int main()
{
int n,m;
int a[40][40]={0};
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
int i;
int j;
memset(a,0,sizeof(a));
i=0;
j=m/2;
int k=1;
while(k<=m*m)
{
if(i<0&&j<m){//i在矩阵外面
i=m-1;
continue;
}
if(j>=m&&i>=0){//j在矩阵外面
j=0;
continue;
}
if(a[i][j]==0){//符合条件
a[i][j]=k;
i-=1;
j+=1;
}else{//该位置有数值了
i+=2;
j-=1;
continue;
}
k++;
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
}
return 0;
}
杭电ACM 1998奇数阶魔方
时间: 2024-10-13 16:36:16
杭电ACM 1998奇数阶魔方的相关文章
HDU ACM 1998奇数阶魔方
方法: 1.第一个数填第一行正中间. 2.以后依次往上一行后一列填,并遵循如下规则: a.如果往上超出第一行则往最后一行开始: b.如果往右超出最后一列则往第一列开始: c.右上角填后要往下一行开始,列不变: d.如果所要填数之前已填,则往他下面填. 3.循环第二步,直到方格填满. #include<iostream> using namespace std; int main() { int a[20][20]; int T,n,i,j,k; cin>>T; while(T--)
Hdu 1998 奇数阶魔方
奇数阶魔方 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4629 Accepted Submission(s): 2622 Problem Description 一个 n 阶方阵的元素是1,2,...,n^2,它的每行,每列和2条对角线上元素的和相等,这样的方阵叫魔方.n为奇数时我们有1种构造方法,叫做"右上方" ,
杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY
杭电ACM题目分类
杭电ACM题目分类 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028. 1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092. 1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196. 1197.1201.1202.1205.1219.1234.123
杭电acm 1034题
Problem Description A number of students sit in a circle facing their teacher in the center. Each student initially has an even number of pieces of candy. When the teacher blows a whistle, each student simultaneously gives half of his or her candy to
杭电ACM Java实现样例
若使用Java求解杭电ACM,答案提交必须注意两点: 1.类名一定得是Main,否则服务器无法编译: 2.必须用while进行输入判断. 以1000题测试题为例,代码如下: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); while(scan.hasNextInt()) { int a=scan.n
杭电ACM水仙花数
水仙花数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 96473 Accepted Submission(s): 28632 Problem Description 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的: "水仙花数"是指一个三位数,它的各位数字的立方和等于其本身,比如:1
杭电 acm 2053 ( Switch Game )
这题思路: 一开始有n盏灯,且全部为关闭状态,都记为 0 就是 The initial condition : 0 0 0 0 0 … 然后之后进行i操作就是对这些灯以是否能被i整除,进行改变状态,如将 0 改为 1 或 将 1 改为 0 正如提醒里的 After the first operation : 1 1 1 1 1 … After the second operation : 1 0 1 0 1 … After the third operation : 1 0 0
杭电acm 2095 find your present (2)
#include<iostream>using namespace std;int main(){ int n,x,y; while(cin>>n,n) { cin>>x; while(--n) { cin>>y; x=x^y; } cout<<x<<"\n&q