1.用蛮力解这道题也能AC,虽然测试数据能过但是还是AC了,然后看了一些特殊的数据 24 29 34 2 21251,在修改了一下自己的代码加了一个else语句,就AC了,可是自己都有点不太明白。晕了!!!!
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
#define Tp 23
5
#define Te 28
6
#define Ti 33
7
8
int main ()
9

{
10
int p, e, i, d;
11
12
int num = 1;
13
14
while (scanf ("%d%d%d%d", &p, &e, &i, &d) != EOF && (p != -1 && e != -1 && i != -1 && d != -1))
15
{
16
int j;
17
20
for ( j = 1; j <= 21252; j ++)
21
{
22
if ( ((j - p) % Tp == 0) && ((j - e) % Te == 0) && ((j - i) % Ti ==0) )
23
{
24
if ( j - d > 0)
25
printf ("Case %d: the next triple peak occurs in %d days.\n", num, j - d);
26
else
27
printf ("Case %d: the next triple peak occurs in %d days.\n", num, j - d + 21252);
28
29
break;
30
}
31
}
32
num ++;
33
}
34
//system ("pause");
35
return 0;
36
}
37
2.用中国剩余定理解题
显然下一次高峰出现的时间减去给定的p e i 对周期求余==0;所以利用同余可转化;
x%Tp = p % Tp = a; x%Te= e % Te = b; x%Ti = i % Tpi= c;
天啊!WA了N次居然是因为公式没理解好 求x时出错了。
1
//2.用中国剩余定理解
2
#include <stdio.h>
3
#include <stdlib.h>
4
int main ()
5

{
6
int p, e, i , d, x;
7
int Tp = 23, Te = 28, Ti = 33;
8
int num = 1;
9
while (scanf ("%d%d%d%d", &p, &e, &i, &d ) != EOF && (p != -1 && e != -1 && i != -1 && d != -1) )
10
{
11
int a = p % Tp;
12
int b = e % Te;
13
int c = i % Ti;
14
15
int n1, n2, n3;
16
17
for (int j = 1; j < 33; j ++)
18
{
19
if ( (23 * 28 * j) % 33 == 1)
20
{
21
n1 = j;
22
break;
23
}
24
25
}
26
for (int j = 1; j < 28; j ++)
27
{
28
if ( (23 * 33 * j) % 28 == 1)
29
{
30
n2 = j;
31
break;
32
}
33
34
}
35
for (int j = 1; j < 23; j ++)
36
{
37
if ( (33 * 28 * j) % 23 == 1)
38
{
39
n3 = j;
40
break;
41
}
42
43
}
44
45
46
47
//x = ( n1 * c + n2 * b + n3 * a ) % (23 * 33 * 28);
x = (28 * 23 * n1 * c + 23 * 33 * n2 * b + 28 * 33 * n3 * a ) % (23 * 33 * 28);
48
49
if ( x - d > 0)
50
printf ("Case %d: the next triple peak occurs in %d days.\n", num, x - d);
51
else
52
printf ("Case %d: the next triple peak occurs in %d days.\n", num, x - d + 21252);
53
54
num++;
55
}
56
//system ("pause");
57
return 0;
58
}
59
posted on 2010-08-06 15:58
雪黛依梦 阅读(177)
评论(0) 编辑 收藏 引用