# 枚举——生理周期

## 2. 生理周期

• 问题描述 人有体力、情商、智商的高峰日子，它们分别每隔23天、28天和33天出现一次。对于每个人，我们想知道何时三个高峰落在同一天。给定三个高峰出现的日子p，e和i(不一定是第一次高峰出现的日子)，再给定另一个指定的日子d，你的任务是输出日子d之后,下一次三个高峰落在同一天的日子(用距离d的天数表示)。例如：给定日子为10，下次出现三个高峰同一天的日子是12,则输出2。
• 输入 输入四个整数：p，e，i和d。p，e，i分别表示体力、情感和智力高峰出现的日子。d是给定的日子，可能小于p，e或i。所有给定日子是非负的并且小于或等于365，所求的日子小于或等于21252。
• 输出 从给定日子起，下一次三个高峰同一天的日子(距离给定日子的天数)。
• 输入样例 0 0 0 0 0 0 0 100 5 20 34 325 4 5 6 7 283 102 23 320 203 301 203 40 -1 -1 -1 -1 四个-1表示输入结果，四个数字分别表示p，e，i，d。
• 输出样例 Case 1: the next triple peak occurs in 21252 days. Case 2: the next triple peak occurs in 21152 days. Case 3: the next triple peak occurs in 19575 days. Case 4: the next triple peak occurs in 16994 days. Case 5: the next triple peak occurs in 8910 days. Case 6: the next triple peak occurs in 10789 days.
• 解法一
#!/usr/bin/env python
# _*_ coding: utf-8 _*_

input_data = [[0, 0, 0, 0],
[0, 0, 0, 100],
[5, 20, 34, 325],
[4, 5, 6, 7],
[283, 102, 23, 320],
[203, 301, 203, 40]]
max_days = 21252
p_circle = 23
e_circle = 28
i_circle = 33

for data in input_data:
p = data[0]
e = data[1]
i = data[2]
d = data[3]
for day in xrange(d + 1, max_days + 1):
if abs(day - p) % p_circle == 0 and abs(day - e) % e_circle == 0 and abs(day - i) % i_circle == 0:
print 'the next triple peak occurs in %d days.' % (day - d)
break
• 输出
the next triple peak occurs in 21252 days.
the next triple peak occurs in 21152 days.
the next triple peak occurs in 19575 days.
the next triple peak occurs in 16994 days.
the next triple peak occurs in 8910 days.
the next triple peak occurs in 10789 days.
• 用时
executed in 32ms

• 方法二
#!/usr/bin/env python
# _*_ coding: utf-8 _*_

input_data = [[0, 0, 0, 0],
[0, 0, 0, 100],
[5, 20, 34, 325],
[4, 5, 6, 7],
[283, 102, 23, 320],
[203, 301, 203, 40]]
max_days = 21252
p_circle = 23
e_circle = 28
i_circle = 33

for data in input_data:
p = data[0]
e = data[1]
i = data[2]
d = data[3]

circles = (max_days - i) // i_circle

for circle in xrange(1, circles + 1):
day = i + i_circle * circle
if (day - p) % p_circle == 0 and (day - e) % e_circle == 0:
print 'the next triple peak occurs in %d days.' % (day - d)
break
• 输出
the next triple peak occurs in 21252 days.
the next triple peak occurs in 21152 days.
the next triple peak occurs in 19575 days.
the next triple peak occurs in 16994 days.
the next triple peak occurs in 8910 days.
the next triple peak occurs in 10789 days.
• 用时
executed in 17ms

## 参考资料

1. 程序设计与算法（二）算法基础

440 篇文章56 人订阅

0 条评论

## 相关文章

2305

1222

3458

### 【c语言】简单学生信息管理系统

1.有10个学生，每个学生的数据包括学好、姓名、4门课的成绩、总成绩和平均成绩。从键盘输入10个学生的数据（包括学好、姓名以...

1.2K1

2578

3684

1273

### 2924 数独挑战

2924 数独挑战  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果 题目描述 Descripti...

2963

4118

### POJ2318 TOYS 判断点与直线位置关系 【计算几何】

Calculate the number of toys that land in each bin of a partitioned toy box.

1133