Candy Bags

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status Practice CodeForces 334A

Description

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.

Sample Input

2


1 4
2 3


分析:对角线问题,根据对角线进行运算。

代码:

#include<stdio.h>
#include<string.h>
int main()
{
    int n,a[10001],i,j,x,f;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
            a[i]=i;
        f=0;
        for(i=1;i<=n;i++)
        {
            x=i;
            f=0;
            for(j=1;j<=i;j++)
            {
                if(f==0)
                {
                    printf("%d",x);
                    f=1;
                }
                else
                {
                    printf(" %d",x);
                }
                x+=n-1;
            }
            x=n*(i+1);
            for(j=i+1;j<=n;j++)
            {
                if(x>n*n)
                   break;
                printf(" %d",x);
                x+=n-1;
            }
            printf("\n");
        }
    }
}

另一种方法:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAXSIZE 100100
#define eps 1e-8
#define LL __int4
#define N 110

int a[N][N];

int main()
{
    int n;
    int i,j;
    while(~scanf("%d",&n)){
            int cnt = 1;
        for(i = 0;i<n;i++)
            for( j = 0;j<n;j++){
                a[i][j] = cnt;
                cnt+=1;
            }

           //  for(i = 0;i<n;i++)
          //  for( j = 0;j<n;j++)
               // printf("%d\n",a[i][j]);
            int flag ;
            int sum = 0;
            for(j = 0;j<n;j++){
                    flag = 1;
                for( i = 0;i<n;i++){
                        if(i+j>=n)
                          sum = i+j-n;
                        else
                          sum = i+j;
                    if(flag == 1){
                       printf("%d",a[i][sum]);
                       // printf("%d %d",i,sum);
                        flag = 0;
                    }
                    else{
                      printf(" %d",a[i][sum]);
                      //  printf(" %d %d",i,sum);
                    }
                }
                printf("\n");
            }
        }

    return 0;
}

Candy Bags

时间: 2024-10-11 04:02:24

Candy Bags的相关文章

所谓的日常 #7 - 袁紹磐河戰公孫 孫堅跨江擊劉表

div.2 CodeForces 334A Candy Bags 给定一个偶数n(<=100),把价值为1~n^2的糖们分发给n个小朋友,使得大家手上糖的总价值一样. 考虑这样配对:(1,n^2),(2,n^2-1),(3,n^2-2)...(i,n^2-i+1).每对价值和都是n^2+1,总共有n^2/2对,按对分给小朋友就好啦. 1 #include <stdio.h> 2 3 int main() { 4 int n; 5 scanf("%d",&n);

Not Too Impressed With The Runway Bags This Year

I B? These laptop bags pupils a strong, durable and resistant to exposure from day to day, for students to go to bags, such as laptop computers remain safe. Tho laptop bags are designed for students and managers must be durable, students also love ba

The Bundle Of The Herb Can Be Placed In Paper Bags

They can usually be purchased in three sizes: £ 5, £ 1 and a half pound bags. I even saw them to buy on eBay. Many companies are cutting Tekpak bag of coffee Solutions. The grass can grow, but not weed. In many instances, I know I would be very grate

When Things Get Very Creative With The Iconic Bags

Although laptop bags shoulder should be well prepared, should not be too great because it allows you to move freely in the laptop. If it is possible to store a computer belonging to ensure that it is suitable for the bags. With so many different bran

It Is Essential To Use Cellophane Bags To Protect These

Like most moms today are trying to reduce the amount of sugar and processed foods your child is in contact with the challenge is to come up with gadgets that children and mothers agreed. Try packet size kid snack healthy snacks, such as popcorn, drie

[LeetCode][Java] Candy

题目: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more cand

从视频文件中读入数据--&gt;将数据转换为灰度图--&gt;对图像做candy边缘检测

//从视频文件中读入数据-->将数据转换为灰度图-->对图像做candy边缘检测 //作者:sandy //时间:2015-10-10 #include <cv.h> #include <highgui.h> int main(int argc, char *argv[]){ //预备工作 CvCapture* capture=cvCreateFileCapture("E:\\Videos\\xx.avi");//让capture变量指向视频文件 i

[leet code 135]candy

1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

LeetCode 笔记25 Candy (艰难的调试)

There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies