天天看點

zju2007EXCEL排序

http://acm.hdu.edu.cn/showproblem.php?pid=1862

浙大計算機研究所學生複試上機考試-2007年

 sort函數,O(n*logn)  最大複雜度100000*log100000 也不會超過時間(1s)

#include <stdio.h>
#include <algorithm>        //sort()函數
#include <string.h>         //strcmp()函數
using namespace std;

struct E 
{
	char no[10];
	char name[10];
	int mark;
};
E stu[100005];                  //最多100000個學生

bool cmp1(E A,E B)
{
	return strcmp(A.no,B.no)<0;        //字元串比較用strcmp()函數
}
bool cmp2(E A,E B)
{
	int tmp=strcmp(A.name,B.name);     //名字相同時再比較學号
	if (tmp!=0)
	{
       return tmp<0;
	}
	else
		return strcmp(A.no,B.no)<0;
}
bool cmp3(E A,E B)
{
	if (A.mark!=B.mark)
	{
		return A.mark<B.mark;
	}
	else
		return strcmp(A.no,B.no)<0;
}
int main()
{
	int n,m,i;
	int ans=0;
  while (scanf("%d%d",&n,&m)!=EOF && n!=0)
  {
	  ans++;
	for(i=1;i<=n;i++)
	{
		scanf("%s%s%d",stu[i].no,stu[i].name,&stu[i].mark);
	}
	if (m==1)
	{
		sort(stu+1,stu+1+n,cmp1);
	}
    else if (m==2)
    {
        sort(stu+1,stu+1+n,cmp2);  
    }
	else if (m==3)
	{
		sort(stu+1,stu+1+n,cmp3);
	}
	printf("Case %d:\n",ans);
	for (i=1;i<=n;i++)
	{
		printf("%s %s %d\n",stu[i].no,stu[i].name,stu[i].mark);
	}
  }
  return 0;
}
           

繼續閱讀