Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

AC过的大牛帮我看看,我的老是WA。

Posted by 19871224 at 2009-04-01 22:45:20 on Problem 3339
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Player
{
	int number;
	char name[21];
	char role[2];
	int year;
}Player; 

//获取时间,比如1998-2006应该返回9
int GetYears(char* p)
{
	const char* p1 = strtok(p, "-");
	const char* p2 = strtok(NULL, "-");
	return atoi(p2)-atoi(p1)+1;
}
//解析阵型 比如4-4-2解析为4,4,2
int ParseArrangement(char* p, int* d, int* m, int* s){
	*d = atoi(strtok(p, "-"));
	*m = atoi(strtok(NULL, "-"));
	*s = atoi(strtok(NULL, "-"));
	return 0;
}

//将队员按照编号由小到大排序
int cmp(const void* p1, const void* p2){
	Player* pa = (Player*)p1;
	Player* pb = (Player*)p2;
	return (pa->number - pb->number);
}

//得到被选中的11名队员在数组中的索引
int* GetPlayers(Player* p, int d, int m, int s){
	int *player = (int*)malloc(sizeof(int)*11); 
	int n = 0;
	int i;
	//找出守门员
	for( i=0; i<22; i++){
		if(p[i].role[0] == 'G'){
			player[n++] = i;
			if(n == 1)
				break;
		}
	}
	if(n<1)
		goto Fail;
    //找出'D'
	for(i=0; i<22; i++){
		if(p[i].role[0] == 'D'){
			player[n++] = i;
			if(n == d+1)
				break;
		}
	}
	if(n < d+1)
		goto Fail;

	//找出'M'
	for(i=0; i<22; i++){
		if(p[i].role[0] == 'M'){
			player[n++] = i;
			if(n == d+m+1)
				break;
		}
	}

	if(n < d+m+1)
		goto Fail;
	//找出'S'
	for(i=0; i<22; i++){
		if(p[i].role[0] == 'S'){
			player[n++] = i;
			if(n==11)
				break;
		}
	}

	if(n<11)
		goto Fail;

	return player;

Fail:
	free(player);
	return NULL;
}

int Arrangement(Player* p, int d, int m, int s){
	int* pa = GetPlayers(p, d, m, s);
	if(pa == NULL)
		printf("IMPOSSIBLE TO ARRANGE \n");
	else{

		//先搜索出时间最长的队员,即为队长
		int max = 0;   
		int pos=0;
		for(int i=0; i<11; i++)
		{
			
			if(p[pa[i]].year >= max){
				max = p[pa[i]].year;
				pos = i;
			}
		}

		printf("%d %s %s\n", p[pa[pos]].number, p[pa[pos]].name, p[pa[pos]].role);

		
		for(int j=0; j<11; j++){
			if( j != pos) //队长已经选出,现在要排除
				printf("%d %s %s\n", p[pa[j]].number, p[pa[j]].name, p[pa[j]].role);
		}
		free(pa);
	}
	return 1;
}

int main()
{
	Player player[22];
	char tmp[10];
	scanf("%s", tmp);
	while(strcmp(tmp, "0")){
		for(int i=0; i<22; i++){
			player[i].number = atoi(tmp);
			scanf("%s", player[i].name);
			scanf("%s", player[i].role);
			player[i].year = 0;
			
			scanf("%s", tmp);
			while(strlen(tmp) == 9){
				player[i].year += GetYears(tmp);
				scanf("%s", tmp);
			}
		}
		int d, m, s;
		ParseArrangement(tmp, &d, &m, &s);
		qsort(player, 22, sizeof(Player), cmp);
		
		Arrangement(player, d, m, s);
		printf("\n");
		scanf("%s", tmp);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator