zoukankan      html  css  js  c++  java
  • Candy Bags

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

    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 N;
    int g[125][125];
    int main()
    {
        int i,j,k;
        scanf("%d",&N);
        for (i=1;i<=N;i++) g[1][i]=i;
        for (i=2;i<=N;i++)
        {
            for (j=1;j<=N;j++)
            {
                if (j==1) g[i][j]=g[i-1][N];
                else g[i][j]=g[i-1][j-1];
            }
        }
        for (i=1;i<=N;i++)
        {
            int tmp=0;
            for (j=1;j<=N;j++)
            {
                printf("%d ",g[i][j]+tmp);
                tmp+=N;
            }
            printf("
    ");
        }
        return 0;
    }
    

     youcuowu

    #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("
    ");
            }
        }
    }


    另一种方法:

    #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
    ",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("
    ");
                }
            }
    
        return 0;
    }
  • 相关阅读:
    ubuntu18.04安装dash-to-dock出错的问题
    使用SVN+Axure RP 8.0创建团队项目
    软件工程实践专题第一次作业
    C#单问号(?)与双问号(??)
    词根 ten 展开 持有 /tin/tent/tain “to hold”
    vscode 对js文件不格式化的修正方案 settings.json
    open cv java 可以 对图片进行分析,得到数据。考试答题卡 2B铅笔涂黑嘎达 识别
    bounties 赏金 bon = good 来自法语 bonjour 早上好
    class cl表示 汇聚 集合 ss表示 阴性 这里表示抽象
    git svn 提交代码日志填写规范 BUG NEW DEL CHG TRP gitz 日志z
  • 原文地址:https://www.cnblogs.com/lipching/p/3904061.html
Copyright © 2011-2022 走看看