【 OJ 】 HDOJ1036 18年11月17日15:46 [ 33 ]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QingCoffe/article/details/84189580

这题被题目意思搞了个半死.....

后来发现意思为接力赛分n段跑,总距离d ,每个队伍t 的跑的时间分2段 总时间就是2个加起来 ,求平均每km的时间 (四舍五入)

突然发现不会用C++从....string里面读取出来h:m:s...一个个读取取出来好烦人....参考了人家c的....尼玛一个sscanf解决了 

AC代码:

# include<iostream>
# include<string>
# include<iomanip>
#pragma warning (disable:4996)
using namespace std;
int main(void) {
	int n;
	double d;
	cin >> n >> d;//输入分n段 总距离d
	int t;//队伍号
	string t1;// 读取跑的时间
	int sum,temp1,temp2,temp3;
	bool isok;
	while (cin >> t) {
		sum = 0;
		isok = true;
		for (int i = 0; i < n; ++i) {
			cin >> t1;
			sscanf(t1.c_str(), "%d:%d:%d", &temp1, &temp2, &temp3);
			if (t1[0] == '-') {//被取消资格
				isok = false;
			}
			else {
				sum += (temp1 * 3600 + temp2 * 60 + temp3);
			}
		}//录入总时间
		if (isok) {
			printf("%3d: %d:%02d min/km", t, (int)(sum / d + 0.5) / 60, (int)(sum / d + 0.5) % 60);//0.5是四舍五入,强转int丢失精度
			cout << endl;//不知道为啥把\n写printf里面出不来...
		}
		else 
			cout <<setw(3)<< t << ": -" << endl;
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/QingCoffe/article/details/84189580