hdu 1220 Cube (数学)




Problem Description


Cowl is good at solving math problems. One day a friend asked him such a question: You are given a cube whose edge length is N, it is cut by the planes that was paralleled to its side planes into N * N * N unit cubes. Two unit cubes may have no common points or two common points or four common points. Your job is to calculate how many pairs of unit cubes that have no more than two common points.
Process to the end of file.




There will be many test cases. Each test case will only give the edge length N of a cube in one line. N is a positive integer(1<=N<=30).




For each test case, you should output the number of pairs that was described above in one line.
 思路:      对于任何两个单位立方体,交点个数的情况有0个、1个、2个、4个;2个公共点时,有一条公共边;4个公共点时,有一个公共面。
             这题 求不多于两个公共点的单位立方体的对数,则可先求出4个公共点的单位立方体的对数。
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,m;
 5     while(~scanf("%d",&n))
 6     {
 7         m=(n*n*n*(n*n*n-1)/2)-(6*n*n*n-6*n*n)/2;
 8         printf("%d\n",m);
 9     }
10 }
View Code