334A Candy Bags
1 second
256 megabytes
standard input
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.
The single line contains a single integer n (n is even, 2 ≤ n ≤ 100) — the number of Gerald's brothers.
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.
2
1 4
2 3
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的更多相关文章
- codeforces 334A - Candy Bags
忘了是偶数了,在纸上画奇数画了半天... #include<cstdio> #include<cstring> #include<cstdlib> #include ...
- Candy Bags
读懂了题就会发现这是个超级大水题 Description Gerald has n younger brothers and their number happens to be even. One ...
- A. Candy Bags
A. Candy Bags http://codeforces.com/problemset/problem/334/A time limit per test 1 second memory l ...
- F - Candy Bags
A. Candy Bags time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces A. Candy Bags 解题报告
题目链接:http://codeforces.com/contest/334/problem/A 题意:有n个人,将1-n袋(第 i 袋共有 i 颗糖果,1<= i <=n)所有的糖 ...
- Candy Distribution
Kids like candies, so much that they start beating each other if the candies are not fairly distribu ...
- CodeForces Round 194 Div2
A. Candy Bagstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputs ...
- 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 ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- 简单的DbContext工厂类(EFCore)
前言 根据appsettings.json的中配置的数据库类型,使用工厂模式创建DbContext 代码实现 appsettings.json中的配置项 //使用的数据库类型 "Server ...
- 解决windows与虚拟机ubuntu互相ping不通的问题
工作中经常用Ubuntu开发,而Ubuntu是安装在虚拟机中的,在弄网络开发的时候经常会用windows下的网络调试工具与Ubuntu中写好的网络程序进行通信,首先要保证windows与Ubuntu能 ...
- 1、进程管理常用命令和进程ID
常用命令 1. ps (英文全拼:process status)命令用于显示当前进程的状态,类似于 windows 的任务管理器. 详细介绍参照:https://www.runoob.com/linu ...
- 如何用OKR促进跨团队协同
https://mp.weixin.qq.com/s/347dKRlez0_KJKGOkTI0AQ
- vscode远程开发安装
https://www.cnblogs.com/xiaoqi/p/vs-code-remote.html ============================= https://blog.csdn ...
- MarkDown学习笔记 Typora
快捷方式篇 新建 ctrl + N 新建窗口 ctrl + shift + N 打开md文件 ctrl + O 快速打开 ctrl + P 保存 ctrl + S 另存为 ctrl + shift + ...
- nginx及其常用实践方案
nginx及其常用实践方案 1.概述 1.1 什么是nginx? 1.2 什么是反向代理? 2.nginx常用命令 3.ningx配置实践 3.1 nginx.conf基础配置项 3.2 http 反 ...
- Prometheus-process exporter-进程监控
Prometheus-process exporter-进程监控 1.下载安装 2.修改配置文件 3.启动 相关内容原文地址: 博客园:落烨无痕:process exporter 配置项解释 huan ...
- Mysql数据库用户及用户权限管理,Navicat设置用户权限
Mysql数据库用户及用户权限管理,Navicat设置用户权限 一.Mysql数据库的权限 1.1 mysql数据库用户权限级别 1.2 mysql数据库用户权限 1.3 存放用户权限表的说明 二.用 ...
- 利用powershell隐藏执行后门
运行后门时,有些后门不能中断.双击运行这种后门会产生一个黑框. 一条命令就能使其在后台执行 命令解释: start-process启动一个进程 -windowstyle窗口样式 hidden隐藏