先把所给数据换成秒,然后算出当前时针与分针相差的角度,再算出 一秒钟分针与时针各能转多少度,然后每过一秒,时针与分针之间的角度缩小或者扩大的角度是一定的,然后直接O(1)出结果即可;这题数据水的一批,随随便便一个错误代码就能过,但是分类讨论绝对是正确的;
AC代码
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int re_miao,re_fen,re_shi;
void judge(int a,int hh,int mm,int ss)
{
int jin=0;
double du_cha;
int miao=hh*3600+mm*60+ss;
double fen_angle=miao/(1.0*10);
double shi_angle=miao/(1.0*120);
int t=fen_angle;
double re=fen_angle-t;
int t1=shi_angle;
double re1=shi_angle-t1;
t=t%360;
t1=t1%360;
double ff_angle=t+re;
double ss_angle=t1+re1;
double cha=1.0/10-1.0/120;
//cout<<ff_angle<<"!!!!"<<ss_angle<<endl;
if (ff_angle>=ss_angle)
{
double c=ff_angle-ss_angle;
if (c<=a)
du_cha=a-c;
else
{
du_cha=360.0-c-a;
double du1=360.0-c;
if (du1==a)
du_cha=0;
else if (du1<a)
{
du_cha=a+du1;
}
//cout<<du_cha<<"++++"<<cha<<"******"<<endl;
}
double need_miao=(du_cha)/cha;
//printf("%.2lf^^^^^\n",need_miao);
re_miao=(int)(ss+need_miao)%60;
jin=int(ss+need_miao)/60;
re_fen=(int)(mm+jin)%60;
jin=(mm+jin)/60;
re_shi=(int)(hh+jin)%12;
//cout<<re_shi<<":"<<re_fen<<":"<<re_miao<<endl;
}
else
{
double c=ss_angle-ff_angle;
if (c>=a)
{
du_cha=c-a;
double du1=360-c;
if (du1<=a)
du_cha=min(du_cha,(a-du1));
}
else
du_cha=c+a;
double need_miao=(du_cha)/cha;
//printf("%.2lf^^^^^\n",need_miao);
re_miao=(int)(ss+need_miao)%60;
jin=int(ss+need_miao)/60;
re_fen=(int)(mm+jin)%60;
jin=(mm+jin)/60;
re_shi=(int)(hh+jin)%12;
//cout<<re_shi<<":"<<re_fen<<":"<<re_miao<<endl;
}
}
int main()
{
int hh,mm,ss,a,ans=1;
char ch1,ch2;
while (cin>>hh>>ch1>>mm>>ch2>>ss)
{
cin>>a;
judge(a,hh,mm,ss);
if (re_shi==hh&&re_fen==mm&&re_miao==ss)
judge(a,hh,mm,ss+1);
cout<<"Case #"<<ans++<<": ";
if (re_shi<10)
cout<<"0"<<re_shi<<":";
else
cout<<re_shi<<":";
if (re_fen<10)
cout<<"0"<<re_fen<<":";
else
cout<<re_fen<<":";
if (re_miao<10)
cout<<"0"<<re_miao<<endl;
else
cout<<re_miao<<endl;
}
return 0;
}