题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1036
题 意:要你计算n个接力队走d段路程所花的平均时间。若有一队时间为-:--:--的话,及时为弃权。
思 路:直接暴力计算即可;但要注意:
1、输入队伍编号的时候要用%3d格式,否则前面的空格就会把输入搞乱了
2、可以把h:mm:ss格式的时间当做字符串输入,存放在一个char数组中,然后用 strtok函数一步一步地把里面的数字分离出来。
3、要把单位换算成 m:ss,min/km,即跑一千米用多少时间。
4、计算结果的过程中注意要四舍五入,不能全部用整形变量,否则会因为误差而Wrong Answer。
5、输出结果时,注意当秒数只有1位数的时候要在前面补0(不能出现4:8这种格式),否则依然Wrong Answer.
代码如下:
#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <climits>
#include <algorithm>
#define maxn 200005
typedef __int64 LL;
int main()
{
int n, h, m, s, a;
double d;
scanf ( "%d %lf", &n, &d );
char c[22],t[100];
while( scanf ( "%d", &a ) != EOF )
{
int sum = 0, f = 0;
for( int i = 1; i <= n; i ++ )
{
scanf ( "%s", c );
if( c[0] == '-' ) f=1;
sscanf ( c, "%d:%d:%d", &h, &m, &s );
sum += h*3600 + m*60 + s;
}
if( f ) printf("%3d: -\n",a);
else {
sum = (int)(sum/d+0.5);
printf("%3d: %d:%2.2d min/km\n",a,sum/60,sum%60);
}
}
return 0;
}