题目出自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=0printf("r=%d, c=%d, ans=%d\n", 3, 3, find(3, 3)); //output n=3, ans=1printf("r=%d, c=%d, ans=%d\n", 4, 4, find(4, 4)); //output n=4, ans=6printf("r=%d, c=%d, ans=%d\n", 5, 5, find(5, 5)); //output n=5, ans=20printf("r=%d, c=%d, ans=%d\n", 6, 6, find(6, 6)); //output n=6, ans=50printf("r=%d, c=%d, ans=%d\n", 7, 7, find(7, 7)); //output n=7, ans=105printf("r=%d, c=%d, ans=%d\n", 8, 8, find(8, 8)); //output n=8, ans=196printf("r=%d, c=%d, ans=%d\n", 9, 9, find(9, 9)); //output n=9, ans=336printf("r=%d, c=%d, ans=%d\n", 10, 10, find(10, 10)); //output n=10, ans=540printf("r=%d, c=%d, ans=%d\n", 100, 100, find(100, 100)); //output n=100, ans=8004150system("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;}
结语:
因为没有正确解答可以比对,可以确定的只有前三个解,所以如果有错误或是有更好的解法都欢迎跟我说。