UVA11470 Square Sums【水题】

Do you know that there are squares within a square. This might seem confusing, but take a look at this.
????Suppose you have a square grid of size 5 × 5 completely filled with integers.

????You can make three squares from the table above... well there are more but we will consider only concentric squares. The squares are denoted using different fonts.
????In this problem you have to find the sum of the numbers of each square.
????For the above case the sums are
5 + 3 + 2 + 7 + 9 + 1 + 4 + 5 + 6 + 1 + 1 + 1 + 4 + 5 + 6 + 3 = 63
7 + 4 + 2 + 3 + 4 + 3 + 4 + 5 = 32
2 = 2
Input
There will be several lines in the input file. Each case starts with an integer n (n ≤ 10) that determines the dimension of the square grid. Each of the next n lines will contain n integers each that will fill the square table in row major order. Input is terminated by a case where n == 0.
Output
Each line of output will start with ‘Case #:’ where # is replaced by the case number. Then you have to output ?(n/2)? space separated numbers that will represent the sums from outer to inner. Follow the sample for exact details.
Sample Input
5
5 3 2 7 9
1 7 4 2 4
5 3 2 4 6
1 3 4 5 1
1 4 5 6 3
1
1
0
Sample Output
Case 1: 63 32 2
Case 2: 1

问题链接UVA11470 Square Sums
问题简述:(略)
问题分析
????对于给定的矩阵从外到里一圈一圈地求和,并且输出结果。
????该题的关键是如何控制圈?如何求和?用4个变量(row1,row2,col1,col2)分别表示开始和结束行、开始和结束列,控制和计算就简单了。需要考虑特例,即只有1个元素的矩阵和最后一圈只剩下1个元素的情况。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11470 Square Sums */

#include <iostream>

using namespace std;

const int N = 10;
int a[N][N];

int main()
{
    int n, caseno = 0;
    while(scanf("%d", &n) != EOF && n) {
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                scanf("%d", &a[i][j]);

        printf("Case %d:", ++caseno);
        int row1 = 0, row2 = n - 1, col1 = 0, col2 = n - 1;
        while(row1 <= row2) {
            int sum = 0;
            if(row1 == row2)
                sum = a[row1][row2];
            else {
                for(int i = row1; i <= row2; i++)
                    sum += a[i][col1] + a[i][col2];
                for(int i = col1 + 1; i < col2; i++)
                    sum += a[row1][i] + a[row2][i];
            }
            printf(" %d", sum);

            row1++, row2--, col1++, col2--;
        }
        printf("\n");
    }

    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10358688.html

时间: 2024-10-31 16:45:56

UVA11470 Square Sums【水题】的相关文章

nyoj 1099 Lan Xiang&#39;s Square (水题)

题目1099 题目信息 运行结果 本题排行 讨论区 Lan Xiang's Square 时间限制:1000 ms  |  内存限制:65535 KB 难度:0 描述 Excavator technology which is strong, fast to Shandong to find Lan Xiang. Then the question comes.. :) for this problem , i will give you four points. you just judge

nyoj-1099-Lan Xiang&#39;s Square(几何,水题)

题目链接 1 /* 2 Name:nyoj-1099-Lan Xiang's Square 3 Copyright: 4 Author: 5 Date: 2018/4/26 9:19:19 6 Description: 7 给4个点,判断是否形成正方形 8 double类型的值比较大小,直接判断==0竟然A了,然而小于1e-6竟然WA 9 */ 10 #include <iostream> 11 #include <cstdio> 12 #include <algorithm

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

USACO CHAPTER 1 1.1 Ride 水题

水题,主要是学习文件输入输出. 1 /* 2 ID: ijustwa1 3 LANG: C++ 4 TASK: ride 5 */ 6 #include<cstdio> 7 #include<cstring> 8 9 using namespace std; 10 11 const int base=16; 12 const int mod=47; 13 14 char s[10]; 15 char t[10]; 16 17 int main() 18 { 19 FILE *fin

HDU 4007 Dave (基本算法-水题)

Dave Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there

poj 1005:I Think I Need a Houseboat(水题,模拟)

I Think I Need a Houseboat Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 85149   Accepted: 36857 Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land,

hdu-5641 King&#39;s Phone (水题)

题目链接: King's Phone Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 418    Accepted Submission(s): 123 Problem Description In a military parade, the King sees lots of new things, including an An

5.1个人赛解题报告(区间dp,按位与或,图论等水题)

这次5.1打了一场个人赛,已经连赛了三周了,有点疲惫感觉,可能自己太水了,每次都有点小紧张. 这次只解出来三道题,然而有一道按位与按位或的水题不知道思路实在是做题太少,还有就是第一题区间DP,也消耗了不少的时间,但是没有成功的写出来,还是不够熟练啊. 下面写报告 A. System Administrator time limit per test 2 seconds memory limit per test 256 megabytes input standard input output

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore