NEFU 大一寒假集训二(排序)2020.01.01

Summary

早上起来有点困,错了很多不该错的地方 >﹏<

Information

No. Title AC/Submit
A 谁考了第k名-排序 150/310
B 奇数单增序列 144/255
C 成绩排序 134/234
D 没必要的排序1 136/207
E 没必要的排序2 66/262
F 老和尚的导员 82/278
G 健忘的老和尚 72/155
H 戏说三国 37/193
I 相约摩洛哥 20/78
J 结构体排序题一 26/36

Problem A: 谁考了第k名-排序 (1481) [150/310]

Tips

简单的结构体排序

Code

#include <bits/stdc++.h>
using namespace std;

struct student
{
	int num;
	double grade;
}s[100];

int cmp(student s1,student s2)
{
	return s1.grade>s2.grade;
}

int main()
{
	int n,k;
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
		cin>>s[i].num>>s[i].grade;
	}
	sort(s,s+n,cmp);
	printf("%d %g",s[k-1].num,s[k-1].grade);
	return 0;
}

Problem B: 奇数单增序列 (1482) [144/255]

Tips

排序之后输出奇数即可
注意:输出带逗号

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,num[500],f=1;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>num[i];
	}
	sort(num,num+n);
	for(int i=0;i<n;i++)
	{
		if(num[i]%2)
		{
			if(!f)printf(",");
			printf("%d",num[i]);
			f=0;
		}
	}
	return 0;
}

Problem C: 成绩排序 (1483) [134/234]

Tips

名字字典序可以用strcmp处理

Code

#include <bits/stdc++.h>
using namespace std;

struct student
{
	char name[21];
	int grade;
}s[20];

int cmp(student s1,student s2)
{
	if(s1.grade!=s2.grade)return s1.grade>s2.grade;
	else return strcmp(s1.name,s2.name)<0;
}

int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s[i].name>>s[i].grade;
	}
	sort(s,s+n,cmp);
	for(int i=0;i<n;i++)
	{
		cout<<s[i].name<<" "<<s[i].grade<<endl;
	}
	return 0;
}

Problem D: 没必要的排序1 (1659) [136/207]

Tips

确实没必要排序

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,k,cnt=0,tmp;
	while(cin>>n>>k)
	{
		cnt=0;
		for(int i=0;i<n;i++)
		{
			cin>>tmp;
			if(tmp)cnt++;
		}
		if(cnt<k)cout<<cnt<<endl;
		else cout<<k<<endl;
	}
	return 0;
}

Problem E: 没必要的排序2 (1650) [66/262]

Tips

桶排还是有必要的吧

Code

#include <bits/stdc++.h>
using namespace std;

int tong[100001]={0};

int main()
{
	int n,k,tmp;
	long long ans=0;
	scanf("%d %d",&n,&k);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&tmp);
		tong[tmp]++;
	}
	tmp=ans=0;
	for(int i=100000;i>=1;i--)
	{
		for(int j=tong[i];j>0;j--)
		{
			ans+=i;
			k--;
			if(k==0)break;
		}
		if(k==0)break;
	}
	printf("%lld",ans);
	return 0;
}

Problem F: 老和尚的导员 (554) [82/278]

Tips

注意:本题含有多组测试数据,虽然题目没说

Code

#include <bits/stdc++.h>
using namespace std;

struct hs
{
	int num;
	int cyy;
	int xxds;
	int gdsx;
	int yy;
	int sum;
}p[100];

int cmp(hs h1,hs h2)
{
	if(h1.sum!=h2.sum)return h1.sum>h2.sum;
	if(h1.cyy!=h2.cyy)return h1.cyy>h2.cyy;
	if(h1.xxds!=h2.xxds)return h1.xxds>h2.xxds;
	if(h1.gdsx!=h2.gdsx)return h1.gdsx>h2.gdsx;
	if(h1.yy!=h2.yy)return h1.yy>h2.yy;
}

int main()
{
	int n;
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
		{
			p[i].num=i+1;
			cin>>p[i].cyy>>p[i].xxds>>p[i].gdsx>>p[i].yy;
			p[i].sum=p[i].cyy+p[i].xxds+p[i].gdsx+p[i].yy;
		}
		sort(p,p+n,cmp);
		for(int i=0;i<n;i++)
		{
			cout<<p[i].num<<" "<<p[i].sum<<endl;
		}
	}
	return 0;
}

Problem G: 健忘的老和尚 (556) [72/155]

Tips

注意:无论是奖励还是惩罚都按照总成绩从低到高输出

Code

#include <bits/stdc++.h>
using namespace std;

struct hs
{
	int grade;
	char name[101];
}p[10000];

int cmp(hs h1,hs h2)
{
	return h1.grade>h2.grade;
}

int main()
{
	int n,m,o;
	while(cin>>n>>m>>o)
	{
		for(int i=0;i<n;i++)
		{
			cin>>p[i].name>>p[i].grade;
		}
		sort(p,p+n,cmp);
		for(int i=m-1;i>=0;i--)
		{
			cout<<p[i].name<<endl;
		}
		for(int i=n-1;i>=n-o;i--)
		{
			cout<<p[i].name<<endl;
		}
	}
	return 0;
}

Problem H: 戏说三国 (873) [37/193]

Tips

注意:智育、德育、武育三个分数,分别以b%,a%,c%的比率计入加权总分

Code

#include <bits/stdc++.h>
using namespace std;

struct people
{
	double sum;
	double zy;
	double dy;
	double wy;
	char name[21];
}p[100000];

int cmp(people p1,people p2)
{
	return p1.sum>p2.sum;
}

int main()
{
	int t,n;
	double a,b,c;
	cin>>t;
	for(int i=0;i<t;i++)
	{
		cin>>n>>a>>b>>c;
		for(int j=0;j<n;j++)
		{
			cin>>p[j].name>>p[j].zy>>p[j].dy>>p[j].wy;
			p[j].zy*=b/100;
			p[j].dy*=a/100;
			p[j].wy*=c/100;
			p[j].sum=p[j].zy+p[j].dy+p[j].wy;
		}
		sort(p,p+n,cmp);
		printf("Case #%d:\n",i+1);
		for(int j=0;j<n;j++)
		{
			printf("%s %.4f %.4f %.4f %.4f\n",p[j].name,p[j].sum,p[j].zy,p[j].dy,p[j].wy);
		}
	}
	return 0;
}

Problem I: 相约摩洛哥 (874) [20/78]

Tips

条件比较多,考虑充分就可以了

Code

#include <bits/stdc++.h>
using namespace std;

struct team
{
	char name[11];
	int ta,tb,tc;
	int time;
	int num;
}t[100000];

int cmp(team t1,team t2)
{
	if(t1.num!=t2.num)return t1.num>t2.num;
	return t1.time<t2.time;
}

int main()
{
	int n,a,b,c;
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
		{
			cin>>t[i].name>>t[i].ta>>t[i].tb>>t[i].tc;
		}
		for(int i=0;i<n;i++)
		{
			cin>>a>>b>>c;
			t[i].time=0;
                        t[i].num=0;
			if(t[i].ta!=-1)
			{
				t[i].ta+=20*(a-1);
				t[i].num++;
				t[i].time+=t[i].ta;
			}
			if(t[i].tb!=-1)
			{
				t[i].tb+=20*(b-1);
				t[i].num++;
				t[i].time+=t[i].tb;
			}
			if(t[i].tc!=-1)
			{
				t[i].tc+=20*(c-1);
				t[i].num++;
				t[i].time+=t[i].tc;
			}
		}
		sort(t,t+n,cmp);
		for(int i=0;i<n;i++)
		{
			cout<<t[i].name<<" "<<t[i].num<<" "<<t[i].time<<endl;
		}
	}
	return 0;
}

Problem J: 结构体排序题一 (1297) [26/36]

Tips

按照条件排好x y就可以了

Code

#include <bits/stdc++.h>
using namespace std;

struct point
{
	int x;
	int y;
}p[100];
int yx,yy,n;

int cmp(point p1,point p2)
{
	if(yx)
	{
		if(p1.x!=p2.x)return p1.x<p2.x;
		if(yy)return p1.y<p2.y;
		else return p1.y>p2.y;
	}
	else
	{
		if(p1.x!=p2.x)return p1.x>p2.x;
		if(yy)return p1.y<p2.y;
		else return p1.y>p2.y;
	}
}

int main()
{
	while(cin>>yx>>yy>>n)
	{
		for(int i=0;i<n;i++)
		{
			cin>>p[i].x>>p[i].y;
		}
		sort(p,p+n,cmp);
		for(int i=0;i<n;i++)
		{
			printf("(%d,%d)\n",p[i].x,p[i].y);
		}
	}
	return 0;
}
发布了32 篇原创文章 · 获赞 104 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/csg999/article/details/103806515