本题比较基础,主要是输入数据的读取和输出格式的调整。
#include <iostream>
#include <cmath>
using namespace std;
int n; //赛段数
double d; //总里程
int t; //组号
int totalTime; //每组总用时
bool flag; //是否为有效成绩
void input(); //读入一组数据
void output(); //输出一组数据
int main()
{
cin >> n >> d;
while (cin >> t)
{
totalTime = 0;
flag = true;
input();
output();
}
return 0;
}
void input()
{
for (int i = 0; i < n; i++)
{
int temp;
char c;
cin >> c;
if (c == '-') //如果用时的第一个字符为'-',则为无效成绩,舍弃其后的6个字符
{
flag = false;
for (int i = 0; i < 6; i++)
cin.get();
}
else //如果成绩有效,则计算总用时(秒)
{
totalTime += ((int)c - 48) * 60 * 60;
cin.get();
cin >> temp;
totalTime += temp * 60;
cin.get();
cin >> temp;
totalTime += temp;
}
}
}
void output()
{
if (t >= 0 && t <= 9) //调整输出格式,也可以用printf
cout << " " << t << ": ";
else if (t >= 10 && t <= 99)
cout << " " << t << ": ";
else
cout << t << ": ";
if (flag == true)
{
double s = totalTime / d;
int speed = round(s); //四舍五入
int minute = speed / 60;
int second = speed - minute * 60;
if (second>=0 && second<=9)
cout << minute << ":0" << second << " min/km" << endl;
else
cout << minute << ":" << second << " min/km" << endl;
}
else
cout << "-" << endl;
}
继续加油。