1

## [C++] [ITSA競賽題目] 計算斜正方形個數 (Counting Tilt Squares)

``````#include <stdio.h>
#include <stdlib.h>

long long find(int r, int c);

int main(void)
{
printf("r=%d, c=%d, ans=%d\n", 2, 2, find(2, 2));
//output n=2,    ans=0
printf("r=%d, c=%d, ans=%d\n", 3, 3, find(3, 3));
//output n=3,    ans=1
printf("r=%d, c=%d, ans=%d\n", 4, 4, find(4, 4));
//output n=4,    ans=6
printf("r=%d, c=%d, ans=%d\n", 5, 5, find(5, 5));
//output n=5,    ans=20
printf("r=%d, c=%d, ans=%d\n", 6, 6, find(6, 6));
//output n=6,    ans=50
printf("r=%d, c=%d, ans=%d\n", 7, 7, find(7, 7));
//output n=7,    ans=105
printf("r=%d, c=%d, ans=%d\n", 8, 8, find(8, 8));
//output n=8,    ans=196
printf("r=%d, c=%d, ans=%d\n", 9, 9, find(9, 9));
//output n=9,    ans=336
printf("r=%d, c=%d, ans=%d\n", 10, 10, find(10, 10));
//output n=10,   ans=540
printf("r=%d, c=%d, ans=%d\n", 100, 100, find(100, 100));
//output n=100,  ans=8004150

system("pause");
return 0;
}

long long find(int r, int c)
{
long long sum = 0;
int n = r < c ? r : c;

for (int x = 1; x < n; x++)
{
for (int y = x + 1; y < n - x ; y++)
{
sum += (r - x - y) * (c - x - y) * 2;
}
}

for (int x = 2; x <= n; x += 2)
{
sum += (r - x) * (c - x);
}

/*for (int x = 1; x < n; x++)
{
sum += (x + r - n) * (x + c - n);
}*/

return sum;
}
``````

### 1 則留言

1

iT邦大神 1 級 ‧ 2017-09-29 08:52:47

fysh711426 iT邦研究生 2 級‧ 2017-09-29 12:46:26 檢舉