PAT-B1015(C/C++代码解析)

1015 德才论 (25 分)
PAT-B1015(C/C++代码解析)
PAT-B1015(C/C++代码解析)
注:排序题

#include <stdio.h>
#include <algorithm>
struct student{
	int id;
	int de;
	int cai;
};
using namespace std;

bool cmp (student a,student b)
{
	if (a.cai+a.de!=b.cai+b.de)
	return a.de+a.cai>b.de+b.cai;
	else if (a.de!=b.de)
	return a.de>b.de;
	else
	return a.id<b.id;
}

int main ()
{
	int n,low,high,i,q1=0,q2=0,q3=0,q4=0;
	scanf ("%d %d %d",&n,&low,&high);
	struct student a1[n],a2[n],a3[n],a4[n],tem[n];
	for (i=0;i<n;i++)
	{
		scanf ("%d %d %d",&tem[i].id,&tem[i].de,&tem[i].cai);
		if (tem[i].de<low||tem[i].cai<low)
		continue;
		else if (tem[i].de>=high&&tem[i].cai>=high)  //第一类 
		{
			a1[q1].id=tem[i].id;
			a1[q1].de=tem[i].de;
			a1[q1].cai=tem[i].cai;
			q1++;
		}
		else if (tem[i].de>=high&&tem[i].cai<high)   //第二类 
		{
			a2[q2].id=tem[i].id;
			a2[q2].de=tem[i].de;
			a2[q2].cai=tem[i].cai;
			q2++;
		}
		else if (tem[i].de<high&&tem[i].cai<high&&tem[i].de>=tem[i].cai)   //第三类 
		{
			a3[q3].id=tem[i].id;
			a3[q3].de=tem[i].de;
			a3[q3].cai=tem[i].cai;
			q3++;
		}
		else    //第四类 
		{
			a4[q4].id=tem[i].id;
			a4[q4].de=tem[i].de;
			a4[q4].cai=tem[i].cai;
			q4++;
		}
	}
	sort (a1,a1+q1,cmp);
	sort (a2,a2+q2,cmp);
	sort (a3,a3+q3,cmp);
	sort (a4,a4+q4,cmp);
    printf ("%d\n",q1+q2+q3+q4);
	for (i=0;i<q1;i++)
	printf ("%d %d %d\n",a1[i].id,a1[i].de,a1[i].cai);
	for (i=0;i<q2;i++)
	printf ("%d %d %d\n",a2[i].id,a2[i].de,a2[i].cai);	
	for (i=0;i<q3;i++)
	printf ("%d %d %d\n",a3[i].id,a3[i].de,a3[i].cai);
	for (i=0;i<q4;i++)
	printf ("%d %d %d\n",a4[i].id,a4[i].de,a4[i].cai); 
	return 0;
}
上一篇:leetcode 141. Linked List Cycle


下一篇:C++不限制猜测次数的猜数字小游戏