hdu 5705 简单分类讨论

本文介绍了一种用于计算从当前时间开始直至时钟指针达到特定角度所需时间的算法。该算法首先将输入的时间转换为秒数,接着计算时针与分针之间的角度差,再进一步计算秒级精度下两指针每秒转动的角度。最终,通过精确计算得出时针与分针达到预设角度所需的秒数,并输出调整后的时、分、秒。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先把所给数据换成秒,然后算出当前时针与分针相差的角度,再算出 一秒钟分针与时针各能转多少度,然后每过一秒,时针与分针之间的角度缩小或者扩大的角度是一定的,然后直接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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值