算法競賽入門經典-課後練習-3-1-2分數統計



#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <fstream>
	using namespace std;
struct Score
{
	int size;
	double points;
};
vector <Score> student;
vector <double> ans;
int main()
{

	freopen("f:\\input.txt", "r", stdin);
	//freopen("f:\\output1.txt", "w", stdout);
	Score score;
	while(cin >> score.points)
	{
		int flag = 1;
		for(int i = 0; i < student.size(); ++i)
		{
			if(student[i].points == score.points)
			{
				student[i].size++;
				flag  = 0;
				break;
			}
		}
		if(flag == 1)
		{
			score.size = 1;
			student.push_back(score);
		}
	}
	int max = 0;
	for(int i = 0; i < student.size(); ++i)
	{
		if(student[i].size > max)
		{
			max = student[i].size;
		}
	}
	for(int i = 0; i < student.size(); ++i)
	{
		if(student[i].size == max)
		{
			ans.push_back(student[i].points);
		}
	}
	sort(ans.begin(), ans.end());
	int first = 1;
	for(int i = 0; i < ans.size(); ++i)
	{
		for(int j = 0; j != max; j++)
		{
			if(first)
			{
				printf("%.2lf", ans[i]);
				first = 0;
			}
			else
			{
				printf(" %.2lf", ans[i]);
			}
		}
	}
	cout << endl;
	cout <<"Time = " << (double)clock() / CLOCKS_PER_SEC << "s" << endl;
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章