这个貌似是小学的问题,今天又重新做了一遍。
问题是在24小时里,时钟和分钟有几次重合。
我们可以这样思考,假设钟表中的一小格的长度为1,那么可以列出如下表格
种类: 秒针 分针 时针
24h路程: 60*60*24 60*24 60*2
时间: 60*60*24 60*60*24 60*60*24
速度: 1格/秒 1/60格/秒 1/720格/秒
然后容易由速度和路程关系得出公式: 1/60*t - 1/720*t = k*60 (t是时间,k是倍数)
然后枚举一下k(1到24就可以了)
附上枚举代码,同时附上三针重合判断,在求出上面的答案前提下,只要判断一下秒针和时针是否走到同位置就可以了
#include <iostream>
using namespace std;
int n;
bool same(double a){
return fabs(a) < 1e-8;
}
void cal(){
double t1 = 1.0/60;
double t2 = 1.0/720;
double h,m,s;
double t3;
for(int k = 1; k < 24;k ++){
t3 = k*60.0/(t1 - t2);
h = t3/60/60;
m = (t3 - int(h)*60*60)/60;
s = t3 - int(h)*60*60 - int(m)*60;
printf("h = %.0lf,m = %.0lf , s = %.0lf\n",h,m,s);
if((int)t3%60 == (int)(t3*t2)%60)
puts("yes");
}
}
int main(){
cal();
system("pause");
return 0;
}
using namespace std;
int n;
bool same(double a){
return fabs(a) < 1e-8;
}
void cal(){
double t1 = 1.0/60;
double t2 = 1.0/720;
double h,m,s;
double t3;
for(int k = 1; k < 24;k ++){
t3 = k*60.0/(t1 - t2);
h = t3/60/60;
m = (t3 - int(h)*60*60)/60;
s = t3 - int(h)*60*60 - int(m)*60;
printf("h = %.0lf,m = %.0lf , s = %.0lf\n",h,m,s);
if((int)t3%60 == (int)(t3*t2)%60)
puts("yes");
}
}
int main(){
cal();
system("pause");
return 0;
}