UVA 1529 - Clock(数论)

时间:2014-04-27 19:28:46   收藏:0   阅读:352

题目链接:1529 - Clock

题意:给定两个时刻,求时针和分针相遇次数。
思路:先把转一圈会相遇的时刻记录下来,这些时刻肯定是固定的,然后由给定的两个时刻a,b,求出12点到a相遇次数c1,12点到b相遇次数c2,ans = c2 - c1
代码:
#include <stdio.h>
#include <string.h>

const double esp = 1e-6;
int h1, m1, h2, m2;
double t1, t2, s[25];

int main() {
    s[0] = 0;
    for (int i = 1; i < 25; i++) {
	s[i] = s[i - 1] + 720.0 / 11;
    }
    printf("Program 3 by team X\n");
    printf("Initial time  Final time  Passes\n");
    while (~scanf("%d%d%d%d", &h1, &m1, &h2, &m2)) {
	t1 = h1 * 60 + m1; t2 = h2 * 60 + m2;
	if (t2 < t1) t2 += 720;
	int ans1, ans2;
	for (int i = 0; i < 24; i++) {
	    if (s[i] < t1)
		ans1 = i;
	    if (s[i] < t2)
		ans2 = i;
	}
	printf("       %02d:%02d       %02d:%02d%8d\n", h1, m1, h2, m2, ans2- ans1);
    }
    printf("End of program 3 by team X\n");
    return 0;
}


UVA 1529 - Clock(数论),码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!