vector<Athlete> bestAthletes(athletes, athletes + numAthletes); sort(bestAthletes.begin(), bestAthletes.end(), [](const Athlete& a, const Athlete& b) { return totalTime(a) < totalTime(b); });
ofstream bestOutfile("best_athletes.txt"); if (!bestOutfile) { std::cerr << "Ошибка открытия файла для записи лучших спортсменов!" << std::endl; delete[] athletes; return 1; }
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Athlete {
string surname;
int min;
int sec;
int tent;
int hund;
};
double totalTime(const Athlete& athlete) {
return athlete.min * 6000 + athlete.sec * 100 + athlete.tent * 10 + athlete.hund;
}
int main() {
setlocale(LC_ALL, "Russian");
const int numAthletes = 10;
Athlete* athletes = new Athlete[numAthletes];
ofstream outfile("athletes.txt");
if (!outfile) {
cout << "Ошибка открытия файла для записи!" << endl;
delete[] athletes;
return 1;
}
for (int i = 0; i < numAthletes; ++i) {
cout << "Введите фамилию спортсмена #" << (i + 1) << ": ";
cin >> athletes[i].surname;
cout << "Введите время (минуты, секунды, десятые, сотые) для " << athletes[i].surname << ": ";
cin >> athletes[i].min >> athletes[i].sec >> athletes[i].tent >> athletes[i].hund;
outfile << athletes[i].surname << " " << athletes[i].min << " " << athletes[i].sec << " " << athletes[i].tent << " " << athletes[i].hund << std::endl;
}
outfile.close();
vector<Athlete> bestAthletes(athletes, athletes + numAthletes);
sort(bestAthletes.begin(), bestAthletes.end(), [](const Athlete& a, const Athlete& b) {
return totalTime(a) < totalTime(b);
});
ofstream bestOutfile("best_athletes.txt");
if (!bestOutfile) {
std::cerr << "Ошибка открытия файла для записи лучших спортсменов!" << std::endl;
delete[] athletes;
return 1;
}
for (int i = 0; i < 6 && i < bestAthletes.size(); ++i) {
bestOutfile << bestAthletes[i].surname << " "
<< bestAthletes[i].min << " "
<< bestAthletes[i].sec << " "
<< bestAthletes[i].tent << " "
<< bestAthletes[i].hund << std::endl;
}
bestOutfile.close();
delete[] athletes; // освобождение динамической памяти
return 0;
}