UVA 696 - How Many Knights (规律)
2013-11-15 20:36
369 查看
How Many Knights |
(b) one row and two columns away from its position. The following diagram illustrates this. The square marked N represents the position of the knight, and the squares marked X indicate the squares that are under attack.
X | X | |||
X | X | |||
N | ||||
X | X | |||
X | X |
each be no larger than 500.
Input
The input consists of pairs of integers giving values for M and N,followed by a pair of zeroes.
Output
For each input pair, display the number of rows and columns in the board, and the number of knights that can be appropriately placed.Sample Input
2 3 5 5 4 7 0 0
Sample Output
4 knights may be placed on a 2 row 3 column board. 13 knights may be placed on a 5 row 5 column board. 14 knights may be placed on a 4 row 7 column board.
题意:给定n * m棋盘,问最多放几个马
思路:找规律, 分情况讨论, n || m == 1 n || m == 2 其他。三种情况。
代码:
#include <stdio.h>
int n, m, ans;
int main() {
while (~scanf("%d%d", &n, &m) && n + m) {
if (n == 1) ans = m;
else if (m == 1) ans = n;
else if (n == 2) ans = m / 4 * 4 + ((m % 4 * 2) >= 4 ? 4 : (m % 4 * 2));
else if (m == 2) ans = n / 4 * 4 + ((n % 4 * 2) >= 4 ? 4 : (n % 4 * 2));
else ans = (m * n + 1) / 2;
printf("%d knights may be placed on a %d row %d column board.\n", ans, n, m);
}
return 0;
}
相关文章推荐
- uva 696 - How Many Knights
- uva 696 - How Many Knights
- UVa 696 How Many Knights (想法题)
- uva 696 - How Many Knights
- UVa 696 - How Many Knights
- UVA 696 How Many Knights
- UVA696 - How Many Knights(数论)
- UVA - 696 How Many Knights
- UVA 696 How Many Knights
- uva 10790 How Many Points of Intersection?(几何规律)
- uva 10518 How Many Calls?(矩阵快速幂)(找规律)
- Uva 10790 - How Many Points of Intersection?
- UVa11038 - How Many O's?(数位dp)
- uva 11668 How Many Teams?
- uva 10183 - How Many Fibs?(斐波那契数)
- uva 10518 How Many Calls?
- UVa 10223 - How many nodes ?
- uva_10061 - How many zero's and how many digits ?
- uva 10183 How Many Fibs
- 数论 - 高精度Fibonacci数 --- UVa 10183 : How Many Fibs ?