這題被題目意思搞了個半死.....
後來發現意思爲接力賽分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;
}