初识 STL 容器案例之(评委打分)

初识 STL 容器案例之(评委打分),第1张

初识 STL 容器案例之(评委打分

案例代码:

#include
using namespace std;
#include
#include
#include
#include
#include  

// 评委打分


// 打印平均分方法
void printAvg(deque d) {
	d.pop_front();  // 去除最低分
	d.pop_back();   // 去除最高分
	int avg = 0;
	for (deque::const_iterator it = d.begin(); it != d.end(); it++) {
		avg += *it;
	}
	cout  << (avg / d.size()) << endl;
}

void test() { 
	// 创建五名选手的容器
	vector> players;

	// 模拟10名评委打分 并将选手添加到容器中
	srand(time(0));
	for (int k = 0; k < 5; k++) {
		vector temp;
		for (int i = 0; i < 10; i++) {
			// rand() % 100 获取100之内的随机数
			temp.push_back(rand() % 40 + 60);
		}
		players.push_back(temp);
	}

	for (int i = 0; i < players.size(); i++) {
		deque temp(players.at(i).begin(), players.at(i).end()); // 拿到每个vector容器中的数据
		sort(temp.begin(), temp.end()); // 使用sort容器排序
		cout << (i + 1) << "号选手的平均分是:";
		printAvg(temp);
	}
}

int main() {
	test();
	system("pause");
	return 0;
}

(g_g) <- (_ o _)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3970354.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存