2018杭电多校第一场1011(hdu 6308)

题解:这题主要会超时,不能用string。可以使用sscanf提取字符串。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<string.h>

using namespace std;

int main()
{
	char a[20];
	int T,hour,min;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&hour);
		scanf("%d",&min);
		scanf("%s",&a);
		int ans = hour*60+min;
		int c;
		double z = -1;
//		sscanf(a+4,"%lf",&z);
		if(a[3] == '+')
		{
			sscanf(a,"%*[^+]+%lf",&z);//表示忽略+前面的字符,取出+之后的字符 
			c = int(z*10 + 0.1);//必须加0.1,否则会出现精度丢失
			c = c*6-8*60;
		}
		else
		{
			sscanf(a,"%*[^-]-%lf",&z);
			c = int(z*10 + 0.1);//必须加0.1,否则会出现精度丢失
			c = -c*6-8*60;
		}
		ans += c;
		ans %= (24*60);//24*60表示一天的分钟数 
		if(ans < 0)
			ans += 24*60;//如果时间向后退,则从24:00开始后退 
		printf("%02d:%02d\n",ans/60,ans%60);		
	} 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/cutedumpling/article/details/81215992