UVA10177 (2/3/4)-D Sqr/Rects/Cubes/Boxes?【数学】

You can see a (4 × 4) grid below. Can you tell me how many squares and rectangles are hidden there? You can assume that squares are not rectangles. Perhaps one can count it by hand but can you count it for a (100 × 100) grid or a (10000 × 10000) grid. Can you do it for higher dimensions? That is can you count how many cubes or boxes of different size are there in a (10 × 10 × 10) sized cube or how many hyper-cubes or hyper-boxes of different size are there in a four-imensional (5 × 5 × 5 × 5) sized hypercube. Remember that your program needs to be very efficient. You can assume that squares are not rectangles, cubes are not boxes and hyper-cubes are not hyper-boxes.
Input
The input contains one integer N (0 ≤ N ≤ 100) in each line, which is the length of one side of the grid or cube or hypercube. As for the example above the value of N is 4. There may be as many as 100 lines of input.
Output
For each line of input, output six integers S2, R2, S3, R3, S4, R4 in a single line where S2 means no of squares of different size in (N × N) two-dimensional grid, R2 means no of rectangles of different size in (N × N) two-dimensional grid. S3, R3, S4, R4 means similar cases in higher dimensions as described before.
Sample Input
1
2
3
Sample Output
1 0 1 0 1 0
5 4 9 18 17 64
14 22 36 180 98 1198

问题链接UVA10177 (2/3/4)-D Sqr/Rects/Cubes/Boxes?
问题简述
    长度为n,计算nn的正方形里有几个正方形,有几个长方形;nnn里有几个正方体,有几个长方体;四维体重有几个1111体,有几个......
问题分析
    nn的正方形里,边长为1的正方形有nn个,边长为2的正方形有(n-1)*(n-1)个,......;边长为1的矩形宽有n种可能,边长为2的矩形宽n-1种位置,所以矩形所有可能边长个数是n(1+n)/2,从中选出两个组成就可以组成一个矩形;......
程序说明:(略)
参考链接:(略)
题记:(略)

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

/* UVA10177 (2/3/4)-D Sqr/Rects/Cubes/Boxes? */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        long long s2 = 0, s3 = 0,s4 = 0, r2, r3, r4, t;
        for(int i=1;i<=n;i++)
        {
            s2+=i*i;
            s3+=i*i*i;
            s4+=i*i*i*i;
        }
        t = n * (n + 1) / 2;
        r2 = t * t;
        r3 = r2 * t;
        r4 = r3 * t;

        printf("%lld %lld %lld %lld %lld %lld\n",s2,r2-s2,s3,r3-s3,s4,r4-s4);
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/10404407.html