如果思路是计算三者重合的时间,那么会让谜题变得很复杂,具体数学分析也可以看下面网址。
http://www.planetseed.com/node/18560
但是这里使用简单一点的思路,思路一变,那么程序就可以写的很简单了,如下:
思路:
1 先解决时针和分针重合问题,如我前面一篇博客,可以看到12小时之内重合12次(算上头尾两次)。
2 然后问题就转换成为在这个12次相遇的时间中,有没有一次在这一个时间中使得分针和秒针重合(或者是时针和秒针重合)?
使用这一思路我们就可以得到如下C++程序:
#include
#include
#include
#include
#include
#include
using namespace std;
struct HMS
{
int hour;
int minute;
int second;
HMS(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s){}
};
HMS secToHour(int s)
{
int h = s / 3600;
s %= 3600;
int m = s / 60;
s %= 60;
return HMS(h, m, s);
}
void meetTime(vector &mt)
{
double vs = 1.0;
double vm = 1.0 / 60.0;
double vh = 1.0 / 60.0 / 12;
for (int i = 1; i
更多精彩内容:http://www.bianceng.cn/Programming/cplus/
验证程序:
#include "Tick.h"
int main()
{
vector mt;
meetTime(mt);
HMS hms;
for (int i = 0; i
输出结果:
![](http://pic1.bianceng.cn/uploads/allimg/14/11/11/0919445062-0.gif)
故此,谜题解决了,大家不用在网上找答案,也不用猜了,这里已经用程序验证过了。