zoukankan      html  css  js  c++  java
  • uva10177 (2/3/4)-D Sqr/Rects/Cubes/Boxes?

    题目大意就是求出2维 、 3维 、4维下正方形中正方形和长方形的个数(正方体和超正方体类似)

    基本就是推出个公式。。正方形是平方和,长方体是 总的长方形个数- 正方形个数 ( 这个想起高中一道数学题比如n条边就用行和列分别选两条边来包围一个长方形 C(2,n+1)*C(2,n+1)。代码里利用了迭代求和。。没有背公式

    蛋疼的是一开始没有对数组初始化一直WA

    Problem J

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

    Input: standard input

    Output: standard output

    Time Limit: 2 seconds

    You can see a (4x4) 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 (100x100) grid or a (10000x10000) 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 (10x10x10) sized cube or how many hyper-cubes or hyper-boxes of different size are there in a four-dimensional (5x5x5x5) 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. 

       

    Fig: A 4x4 Grid

    Fig: A 4x4x4 Cube

     

    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 ( NxN) two-dimensional grid, R2 means no of rectangles of different size in (NxN) 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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <math.h>
     4 using namespace std;
     5 
     6 const int maxn = 105;
     7 unsigned long long s2[maxn],r2[maxn],s3[maxn],r3[maxn],s4[maxn],r4[maxn];
     8 int main()
     9 {
    10     long long  n;
    11 
    12 
    13     s2[0]=0;s3[0]=0;s4[0]=0;
    14 
    15     for(unsigned long long i=1;i<=100;i++)
    16     {
    17         s2[i]=s2[i-1]+i*i;
    18         r2[i]=pow((i+1)*i/2,2)-s2[i];
    19 
    20         s3[i]=s3[i-1]+i*i*i;
    21         r3[i]=pow(i*(i+1)/2,3)-s3[i];
    22 
    23         s4[i]=s4[i-1]+i*i*i*i;
    24         r4[i]=pow(i*(i+1)/2,4)-s4[i];
    25 
    26     }
    27 
    28     while(scanf("%lld",&n)!=EOF)
    29     {
    30          printf("%llu %llu %llu %llu %llu %llu
    ",s2[n],r2[n],s3[n],r3[n],s4[n],r4[n]);
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    【扫盲】i++和++i的区别
    java 字符串String.intern()方法学习
    随机访问和快速访问
    Semaphore信号量深度解析
    CyclicBarrier回环屏障深度解析
    CountDownLatch深度剖析
    静态代理和装饰者模式的区别
    AspectJ之@DeclareParents注解为对象添加新方法
    C#开发上位机常用
    使用Charles进行抓包、篡改请求、设置代理
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3426637.html
Copyright © 2011-2022 走看看