标签:sicily
Time Limit: 1 secs, Memory Limit: 32 MB
There is a rectangular pool table ABCD with side lengths m and n, where m and n are integers with m<n. Bug put the cue ball at corner A and shoot it at a 45 angle to the sides, and it reflects perfectly (at 45 degres) off all sides, and never loses energy.
The table has four pockets, one in each corner, and the ball will be sunk as soon as it hits a corner. The question is : given the integers m and n, how do you tell which pocket the ball hits first, and how many reflections will the ball make on the way?
Assume A,B,C,D is the lower left corner,upper left corner, upper right corner, lower right corner respectively. The length of AB is m, and the length of AD is n.
B---------C
| |
| |
A---------D
Input consist of mutiple cases. Each case include two integers m and n, both of which fits in 32-bit signed integer.
For each case, you should find out which pocket (A,B,C,D) the ball first hit and how many reflections the ball make .
6 10
C 6
假设球撞到边界时不会反弹,而是继续进入下一个与原图一样大小的边界,那么,由于出发的角度是45度,经过了m,n的公倍数的时候就会撞击一次洞,第一次撞击自然就是最小公倍数了(这样的话其实30度60度也是同样的思路)。见下图:
上图已经显示得很清晰了,这样,我们只需计算出最小公倍数,然后计算撞击横边和纵边的次数,最后再根据撞击次数确定洞就行:
#include <stdio.h> int gcd(int a, int b) {//求最大公约数不用递归会快 int c = a % b; while (c != 0) { a = b; b = c; c = a % b; } return b; } int main() { int n, m, lcm, hit_horizontal_size, hit_vertical_size; while (~scanf("%d%d", &m, &n)) { lcm = n * m / gcd(n, m); hit_horizontal_size = lcm / m - 1;//注意由于最后到洞的时候是不算撞边的,所以都要减去一 hit_vertical_size = lcm / n - 1; //1 & 1 = 1, 0 & 1 = 0,判断奇偶这样快 if (hit_horizontal_size & 1) {//如果撞击水平边的次数是奇数次,那么可能的点是AD if (hit_vertical_size & 1) {//如果撞击竖直边的次数是奇数次,那么可能的点是AB printf("A "); } else { printf("D "); } } else { if (hit_vertical_size & 1) { printf("B "); } else { printf("C "); } } printf("%d\n", hit_horizontal_size + hit_vertical_size); } return 0; }
标签:sicily
原文地址:http://blog.csdn.net/u012925008/article/details/44738775