先求每一層的個數,然後求不在同一層的pairs:
#include<stdio.h>
int n;
int get(int x){//求同一層的pairs
int i,j;
int Count=0;
int t=x*x;
for(i=0;i<x-1;i++){
Count+=t-3;
j=0;
t--;
for(j=1;j<x-1;j++)
{
Count+=t-3;
t--;
}
Count+=t-2;
t--;
}
t--;
for(j=0;j<x-1;j++){
Count+=t-1;
t--;
}
return Count;
}
int main(){
int Count,i,m,t;
while(scanf("%d",&n)==1){
m=n*n*n-n*n;
t=get(n);
Count=t*n;
for(i=1;i<n;i++){//求自己和自己上面層數的pairs
Count+=n*n*(m-1);
m-=n*n;
}
printf("%d\n",Count);
}
return 0;
}