时针与分针的相遇问题
问题描述:
一天中时针和分针共相遇多少次?
方法一:
这道题可以转换为中学时学的追及问题,分针走得快,时针走得慢,分针要想追到时针必须要比时针多走360度。分针一分钟走360/60=6度,时针一分钟走360/(12 * 60)=0.5度。那么,分针比时针多走360度需要的时间为360/(6-0.5),所以在一天之中时针和分针的相遇次数为24*60/(360/(6-0.5))=22次。
方法二:
当然我们也可以运用编程的方法来解决这道问题
#include <algorithm>
#include <iostream>
#include <vector>
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<int>& mt) {
double vs = 1.0;
double vm = 1.0 / 60;
double vh = 1.0 / (60 * 12);
for (int i = 1; i < 12; ++i) mt.push_back(i * 60.0 / (vm - vh));
}
#include <stdio.h>
#include "Tick.h"
int main() {
vector<int> mt;
meetTime(mt);
HMS hms;
for (int i = 0; i < mt.size(); ++i) {
hms = secToHour(mt[i]);
cout << mt[i] << " - " << hms.hour << ":" << hms.minute << ":"
<< hms.second << endl;
}
cout << endl;
system("pause");
return 0;
}
运行结果: