Welcome

首页 / 软件开发 / 数据结构与算法 / 求一天中时针和分针会相遇多少次

求一天中时针和分针会相遇多少次2015-09-04如标题题目:

数学分析请看:

http://www.planetseed.com/node/18560

程序:

#include <iostream>#include <vector>#include <string>#include <algorithm>#include <map>#include <math.h>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.0;double vh = 1.0 / 60.0 / 12;for (int i = 1; i < 12; i++){mt.push_back(i * 60.0 / (vm-vh));}}

测试程序:

#include "Tick.h"#include<stdio.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;}
运行结果:

和上面网页中数学计算出来的结果一样的,接近常数的时间效率,不用搜索所有秒。

作者:csdn博客 靖心