Ural 1319 - Hotel

Problem illustration   You programmers are lucky! You don't have to deal with these terrible people – designers… This story happened with me not so long ago. We had an order from a company building a new hotel. One day they brought a sketch to our workshop. They said that THIS was invented by a very cool designer. They said they had paid heaps of money for THIS. So, THIS had to be built. In general, THIS was not a very complex thing. It was just a square set of shelves where a porter puts guests' mail. Usual hotels have usual stands with shelves for this purpose. But this cool designer had turned everything upside down! To be more precise, not exactly upside down, but upon a corner. Moreover, the cells should be numbered from the right to the left, from the top to the bottom, looking at THIS, staying on its corner, of course. Tell me please, how can the master attach the labels with numbers to THIS? He will look on the shelves, staying normally on its side, you know. He will get tangled on the fourth label already! I will get tangled on the seventh, myself… Actually one should make such designers to label the shelves themselves.
— Oh! You are the cool programmer, I know. Couldn’t you help me? I need just a printout of the table with an arrangement of the labels in the cells. But not in such way as THIS will hang on the wall, but as THIS stands on the table of my workshop. Yes, I understand that you are busy, but you are busy every time! Preparations to the Ural Championship, tests, solutions… So what? If you can’t do it yourself – entrust your competitors with this task. They are the best programmers all over the world, aren’t they? I don’t believe that they couldn’t print the desired table having the size of the square! I would never believe it! So… Excellent! I will take the desired printout away after the contest.

Input

The input consists of the only one integer N (1 ≤ N ≤ 100), which is the size of the square.

Output

You are to write a program that outputs the table of numbers, as they would be arranged when THIS would stand in the workshop. The label with number 1 should be in the upper right corner and other numbers should be arranged along the diagonals from the top to the bottom. The label with the last number (N*N) should be in the lower left corner.

Sample

inputoutput
3
4 2 1
7 5 3
9 8 6
Problem Author: Stanislav Vasilyev Problem Source: VIII Collegiate Students Urals Programming Contest. Yekaterinburg, March 11-16, 2004
// Ural Problem 1319. Hotel
// Verdict: Accepted  
// Submission Date: 01:06:08 16 Jan 2014
// Run Time: 0.031s
//  
// 版权所有(C)acutus   (mail: acutus@126.com) 
// 博客地址:http://www.cnblogs.com/acutus/
// [解题方法]  
// 简单题,直接找矩阵规律即可

#include<stdio.h>

int a[100][100];
 
void solve()
{
    int N, i, j, k, M, t;
    scanf("%d", &N);
    M = N * (N + 1) / 2;
    t = 1;
    j = 0;

    k = 1;
    while(k <= M) {
        for(j = N - 1; j >= 0; j--) {
            t = j;
            i = 0;
            while(t <= N - 1) {
                a[i++][t++] = k++;
            }
        }
    }
    for(i = 1; i < N; i++) {
        t = i;
        for(j = 0; j < N - i; ){
            a[t++][j++] = k++;
        }
    }
    for(i = 0; i < N; i++) {
        for(j = 0; j < N; j++) {
            if(j == 0) printf("%d", a[i][j]);
            else printf(" %d", a[i][j]);
        }
        printf("\n");
    }
}
 
int main()
{
    solve();
    return 0;
}

 

转载于:https://www.cnblogs.com/acutus/p/3521857.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值