Gerald has n younger brothers and their number happens to be even. One day he bought n2 candy bags. One bag has one candy, one bag has two candies, one bag has three candies and so on. In fact, for each integer k from 1 to n2 he has exactly one bag with k candies.
Help him give n bags of candies to each brother so that all brothers got the same number of candies.
Input
The single line contains a single integer n (n is even, 2 ≤ n ≤ 100) — the number of Gerald's brothers.
Output
Let's assume that Gerald indexes his brothers with numbers from 1 to n. You need to print n lines, on the i-th line print n integers — the numbers of candies in the bags for the i-th brother. Naturally, all these numbers should be distinct and be within limits from 1 to n2. You can print the numbers in the lines in any order.
It is guaranteed that the solution exists at the given limits.
Examples
Input
2
Output
1 4
2 3
Note
The sample shows Gerald's actions if he has two brothers. In this case, his bags contain 1, 2, 3 and 4 candies. He can give the bags with 1 and 4 candies to one brother and the bags with 2 and 3 to the other brother.
思路:
此题大意是就是分糖果,Gerald有n个弟弟,他有1-n^2个糖果,现在要求每个弟弟分到糖果为n堆,而且必须每n堆的糖果数相等。
例: 2个弟弟,那么有1-4个糖果,总共10个,很明显每个人要有五个,很容易想到是1 4 和2 3 。但是要是大于2呢?
如果是三个弟弟,那么列个3*3的表如下;
(1,1) 1 | (1,2) 2 | (1,3) 3 |
(2,1) 4 | (2,2) 5 | (2,3) 6 |
(3,1) 7 | (3,2) 8 | (3,3) 9 |
于是我们可以发现分法和对应的坐标是:
1 5 9 (1,1),(2,2),(3,3)
2 6 7 (1,2),(2,3),(3,1)
3 4 8 (1,3),(2,1),(3,2)
此时我们发现了规律.第一行永远都是对角线,而后面的规律却和行列式类似。
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<stack>
using namespace std;
typedef long long ll;
int a[105][105];
int main(){
int n;
cin>>n;
int k=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
a[i][j]=k;
k++;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==1&&j!=n){
cout<<a[j][j]<<" ";
}
else if(i==1&&j==n){
cout<<a[j][j]<<endl;
}
else if(i!=1&&j!=n){
int x=j%n;
int y=(i+j-1)%n;
if(x==0)
x=n;
else if(y==0)
y=n;
cout<<a[x][y]<<" ";
}
else{
int x=j%n;
int y=(i+j-1)%n;
if(x==0)
x=n;
else if(y==0)
y=n;
cout<<a[x][y]<<endl;
}
}
}
return 0;
}