1 条题解

  • 0
    @ 2022-11-5 17:14:45

    积分统计

    本蘜蒻最先想到的做法是用一个map,输入字符串直接往里塞second的数值。但是写到输出发现:怎么按照second排序???

    所以本篇题解……很……暴力、麻烦的解决了这个问题。那就是把map里的数据再转存到struct里面再排序再输出。

    上代码

    #include<bits/stdc++.h>
    using namespace std;
    struct student
    {
    	string first;
    	int second;
    }b[210];  //转换用结构体 
    bool cmp(student x,student y)
    {
    	if(x.second!=y.second)
    		return x.second>y.second;
    	else return x.first<y.first;
    }   //比较函数 
    map<string,int> a;   //map 
    int score[6]={0,5,3,1,1,1};  //记录第几名给多少分的数组 
    int main()
    {
    	for(int i=1;i<=5;i++)
    	{
    		int k;
    		cin>>k;  //第i道题共有k人做完 
    		for(int j=1;j<=k;j++)
    		{
    			string x;  //输入前五个人的名字然后加分 
    			if(j<=5)
    			{
    				cin>>x;
    				a[x]+=score[j];
    			}
    			else cin>>x;  //后面的人不需要加分,光输入就行 
    		}
    	}
    	int js=1;
    	for(map<string,int>::iterator it=a.begin();it!=a.end();it++)
    	{
    		if((*it).second!=0)
    		b[js].first=(*it).first;
    		b[js].second=(*it).second;
    		js++;
    	}  //数据转换到结构体里 
    	sort(b+1,b+js,cmp);  //排序 
    	for(int i=1;i<=js-1;i++)
    	{
    		cout<<b[i].first<<" "<<b[i].second<<'\n';
    	} //输出 
    	return 0;
    }
    
    • 1

    信息

    ID
    1124
    时间
    1000ms
    内存
    256MiB
    难度
    3
    标签
    递交数
    43
    已通过
    24
    上传者