Contest_Scoreboard 10258 Uva_OnlineJudge

这道题关键在排序,但是还可以注意一下数字和字母的混合输入的处理。另外,这道题也是一个使用结构编程的例子。解题过程中多用函数,这样不仅结构清晰,而且当程序出BUG时容易调试。
#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef struct
{
	int nNo;/*队名*/
	int nQstSlvNum;/*解题数*/
	int nSumTime;

	int nQstWrgTms[10];

	char cHvSlved[12];/*'N'for have not solved,'Y' for have solved*/
}ACMTEAM;

int IsTeamHvSolvQst(ACMTEAM* team,int teamNo,int teamNum)
{
	int i;
	for(i = 0;i < teamNum;i ++)
	{
		if(team[i].nNo == teamNo)
			return i;
	}

	return -1;

}

void CountForTheTeam(ACMTEAM *team,int teamNo,int nQstNo,int nTime,char cAns,int &count)
{
	int temp;
	temp = IsTeamHvSolvQst(team,teamNo,count);
	if(-1 == temp)/*还没有解出题,现在就开始记录*/
	{
		team[count].nNo = teamNo;
		if(cAns == 'I' && 'N' == team[count].cHvSlved[nQstNo])
			team[count].nQstWrgTms[nQstNo] ++;
		else if(cAns == 'C' && 'N' == team[count].cHvSlved[nQstNo])
		{
			team[count].nSumTime +=nTime;
			team[count].nSumTime += team[count].nQstWrgTms[nQstNo]*20;
			team[count].cHvSlved[nQstNo] = 'Y';
			team[count].nQstSlvNum ++;
		}
		count ++;
	}
	else
	{
		if(cAns == 'I' && 'N' == team[temp].cHvSlved[nQstNo])
			team[temp].nQstWrgTms[nQstNo] ++;
		else if(cAns == 'C' && 'N' == team[temp].cHvSlved[nQstNo])
		{
			team[temp].nSumTime +=nTime;
			team[temp].nSumTime += team[temp].nQstWrgTms[nQstNo]*20;
			team[temp].cHvSlved[nQstNo] = 'Y';
			team[temp].nQstSlvNum ++;
		}
	}
}
/*int cmp(const void* tmp1,const void* tmp2)  //for qsort
{
	ACMTEAM *team_1, *team_2;
	team_1 = (ACMTEAM*)tmp1;
	team_2 = (ACMTEAM*)tmp2;

	if(team_2->nQstSlvNum != team_1->nQstSlvNum) return team_2->nQstSlvNum > team_1->nQstSlvNum;
	if(team_2->nSumTime !=  team_1->nSumTime) return team_2->nSumTime < team_1->nSumTime;
	if(team_2->nNo > team_1->nNo) return -1;
	else return 1;
}*/
int cmp(ACMTEAM team_1,ACMTEAM team_2)// for ChoosenSort
{
	if(team_2.nQstSlvNum != team_1.nQstSlvNum) return team_2.nQstSlvNum > team_1.nQstSlvNum;
	if(team_2.nSumTime !=  team_1.nSumTime) return team_2.nSumTime < team_1.nSumTime;
	if(team_2.nNo > team_1.nNo) return -1;
	else return 1;
}

void swap(ACMTEAM &team_1,ACMTEAM &team_2)
{
	ACMTEAM tmp;
	tmp = team_1;
	team_1 = team_2;
	team_2 = tmp;
}
void ChoosenSort(ACMTEAM *team,int teamNum)
{
	int i,j,k;
	for(i = 0;i < teamNum;i ++)
	{
		k = i;
		for(j = i + 1;j < teamNum;j ++)
		{
			if(cmp(team[k],team[j]) == 1)
				k = j;
		}
		if(k != i)
			swap(team[i],team[k]);
	}
}
void outPut(ACMTEAM *team,int teamNum)
{
	int i;
	for(i = 0;i < teamNum;i ++)
	{
		printf("%d %d %d\n",team[i].nNo,team[i].nQstSlvNum,team[i].nSumTime);
	}
}

int main()
{
	ACMTEAM team[101];

	int testNum;
	scanf("%d",&testNum);
	getchar();getchar();/*input the enter*/
	
	int i,j,cnt;
	int nQstNo;
	int nTime;
	int teamNo;
	char cAnswState;
	char input_str[20];
	
	for(i = 0;i < testNum;i ++)
	{
		memset(team,0,101*sizeof(ACMTEAM));
		for(j = 0;j < 101;j ++)
			memset(team[j].cHvSlved,'N',12*sizeof(char));
		cnt = 0;
		while(gets(input_str) && strlen(input_str))
		{
			sscanf(input_str,"%d %d %d %c",&teamNo,&nQstNo,&nTime,&cAnswState);
			CountForTheTeam(team,teamNo,nQstNo,nTime,cAnswState,cnt);
		}
		
		ChoosenSort(team,cnt);
		//qsort(team,cnt,sizeof(ACMTEAM),cmp);
		outPut(team,cnt);
		if(i != testNum - 1)
			printf("\n");
	}
	return 0;
}

这个题目关键就是一个排序,但是,还要注意数字和字母的混合输入的解决方法。另外,注意qsort的头文件和cmp函数的编写。

你可能感兴趣的:(Contest_Scoreboard 10258 Uva_OnlineJudge)