一、热身 [Cloned] H - 请使用sort

本文介绍了一种算法,用于从多种T恤设计方案中选择最能满足大众口味的组合。通过收集人们对不同设计元素的满意度评分,算法能找出得分最高的设计方案集合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题:

Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA found that he was trapped by all kinds of suggestions from everyone on the board. It is indeed a mission-impossible to have everybody perfectly satisfied. So he took a poll to collect people's opinions. Here are what he obtained: N people voted for M design elements (such as the ACM-ICPC logo, big names in computer science, well-known graphs, etc.). Everyone assigned each element a number of satisfaction. However, XKA can only put K (<=M) elements into his design. He needs you to pick for him the K elements such that the total number of satisfaction is maximized. 

题意:

假设用n种T恤,然后m个人分别对这n种T恤打分,要计算出哪几种T恤的得分和最高。

题解:

创建结构体包括:T恤序号,T恤得分总和

输入数据依次得到T恤的得分总和,排序输出就得了

(这题解真的太糊弄了,主要也是前边的这些题就是模拟一下排序一下就好了)

主要学习一下结构体的sort的用法

就酱

代码:AC

 

#include<iostream>
using namespace std;
struct nom
{
	int No=0;
	double sat=0;
};
int n, m, k;
double **bank;
nom *ans;
 
int main(void)
{
	while (cin >> n >> m >> k)
	{
		bank = new double*[n];
		ans = new nom[m];
		for (int i = 0; i < n; i++)
		{
			bank[i] = new double[m];
			for (int j = 0; j < m; j++)
			{
				cin >> bank[i][j];
				ans[j].No = j+1;
				ans[j].sat += bank[i][j];
			}
		}
		for (int i = 1; i < m; i++)
		for (int j = 0; j < m - i;j++)
		{
			if (ans[j].sat < ans[j + 1].sat)
			{
				nom s = ans[j]; ans[j] = ans[j + 1]; ans[j + 1] = s;
			}
		}
		for (int i = 1; i < k; i++)
		for (int j = 0; j < k - i; j++)
		{
			if (ans[j].No < ans[j + 1].No)
			{
				nom s = ans[j]; ans[j] = ans[j + 1]; ans[j + 1] = s;
			}
		}
		for (int i = 0; i < k; i++)
		{
			cout << ans[i].No ;
			if (i < k - 1)
				cout << ' ';
			else
				cout << endl;
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值