好久没更新了,好忙。
如标题题目:
数学分析请看:
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;
}
运行结果:
和上面网页中数学计算出来的结果一样的,接近常数的时间效率,不用搜索所有秒。
本文介绍了一种使用C++程序高效计算时钟上秒针与分钟及小时针相遇时刻的方法,通过数学公式推导和算法设计,实现了快速查找秒针与其它指针相遇的具体时间点。

2004

被折叠的 条评论
为什么被折叠?



