A. Candy Bags
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

这题很惭愧,看了一个小时没有看出规律T T。

问了朋友,恍然大悟。

这种找规律的题我真的真的找不出来啊太笨了。

本题的规律就是:对角线上的和。

如下图:

n=3;

n=4;

图片来源:http://liyishuai.blog.ustc.edu.cn/candy-bags/

代码如下:

 1 #include<cstdio>
2 int main()
3 {
4 int n,i=1;
5 scanf("%d",&n);
6 for(int j=1;j<=n;j++)
7 {
8 i=j;
9 while(i<=n*n)
10 {
11 printf("%d ",i);
12 if(i%n==0) //在边界上 那下一步就要拐到下一行第一个
13 i+=1;
14 else
15 i+=n+1; //不在边界就斜着走到下一行
16 }
17 printf("\n");
18 }
19 return 0;
20 }

334A Candy Bags的更多相关文章

  1. codeforces 334A - Candy Bags

    忘了是偶数了,在纸上画奇数画了半天... #include<cstdio> #include<cstring> #include<cstdlib> #include ...

  2. Candy Bags

    读懂了题就会发现这是个超级大水题 Description Gerald has n younger brothers and their number happens to be even. One ...

  3. A. Candy Bags

    A. Candy Bags http://codeforces.com/problemset/problem/334/A   time limit per test 1 second memory l ...

  4. F - Candy Bags

    A. Candy Bags time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. codeforces A. Candy Bags 解题报告

    题目链接:http://codeforces.com/contest/334/problem/A 题意:有n个人,将1-n袋(第 i  袋共有 i  颗糖果,1<= i  <=n)所有的糖 ...

  6. Candy Distribution

    Kids like candies, so much that they start beating each other if the candies are not fairly distribu ...

  7. CodeForces Round 194 Div2

    A. Candy Bagstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputs ...

  8. Codeforces Round #194 (Div.1 + Div. 2)

    A. Candy Bags 总糖果数\(\frac{n^2(n^2+1)}{2}\),所以每人的数量为\(\frac{n}{2}(n^2+1)\) \(n\)是偶数. B. Eight Point S ...

  9. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

随机推荐

  1. 深入研究.NET 5的开放式遥测

    OpenTelemetry 介绍 OpenTelemetry是一种开放的源代码规范,工具和SDK,用于检测,生成,收集和导出遥测数据(指标,日志和跟踪),开放遥测技术得到了Cloud Native C ...

  2. 查看Java的汇编指令

    在IDEA配置VM options,打印汇编指令 -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly windows系统 下载插件 hsdis-amd6 ...

  3. Spring入门及IoC的概念

    Spring入门 Spring是一个轻量级的Java开发框架,最早由Robd Johnson创建,目的为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题,它是一个分层的JavaSE/EE轻量级开源 ...

  4. Linux安装MYSQL并部署主从复制集群

    主节点部署 安装数据库 Ubuntu apt-get install mysql-server -y systemctl start mysql systemctl enabled mysql Cen ...

  5. 线上nginx的一次“no live upstreams while connecting to upstream ”分析

    线上nginx的一次"no live upstreams while connecting to upstream "分析 线上nginx的一次"no live upst ...

  6. (Oracle)已有数据表建立表分区—在线重定义

    今天在做数据抽取的时候,发现有一张业务表数据量达到了5000W,所以就想将此表改为分区表.分区表的有点如下: 1.改善查询性能:对分区对象的查询可以仅搜索自己关心的分区,提高检索速度.2.增强可用性: ...

  7. javamail发送邮件,支持yahoo,google,163.com,qq.com邮件发送

    https://www.iteye.com/blog/fangyunfeng-1847352 https://blog.csdn.net/weixin_38465623/article/details ...

  8. 关于ckfinder上传文件时不能根据结果返回自定义操作问题?

    最近项目中为了便于文件的管理,所以CMS项目中使用到了ckfinder插件,但是在使用的过程中,发现其自带的上传事件,如果上传重名的文件,该工具会自动提示错误,显示上传失败.但是如果想要自己去处理重名 ...

  9. 十四:SpringBoot-配置MongoDB数据库,实现增删改查逻辑

    SpringBoot-配置MongoDB数据库,实现增删改查逻辑 1.MongoDB数据库 1.1 MongoDB简介 1.2 MongoDB特点 2.SpringBoot整合MongoDB 2.1 ...

  10. liux 常用操作命令

    tail -f /home/jyapp/apache-tomcat-7.0.59/logs/catalina.out  //查看实施日志 //删除临时目录并且启动服务器 rm -rf /home/jy ...