Contest_Scoreboard 10258 Uva_OnlineJudge

3690 ワード

        ,                      。  ,                。         ,        ,      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
",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("
"); } return 0; }

この問題の鍵はソートですが、数字とアルファベットの混合入力の解決方法にも注意しなければなりません.また,qsortのヘッダファイルとcmp関数の記述に注意する.