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

WHY!!!! WA?????? 哪里错了????

Posted by win1611 at 2010-02-02 16:54:12 on Problem 2153
//题意:找出Li Ming的排名   注意:是总分排名,即每次都要累加分数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct student
{
	int score;
	char name[35];                      //注意:这里的范围要比30大 因为后面还有一个\0也需要空间
};

int cmp(const void *a,const void *b)    //对学生分数进行排序
{
	return ((*(student *)a).score-(*(student *)b).score);
}
int CmpName(const void *a,const void *b)//对姓名进行排序
{
	return strcmp((*(student *)a).name,(*(student *)b).name);
}

student stu1[10010],stu[10010];         //第一个是用来存放此人所有分数之和 第二个是用来存放每次录入的记录
int main()
{
	int i=0,N,M;
	int M_score;
	int num;
	scanf("%d",&N);
	while(i<N){
		getchar();
		gets(stu[i].name);
		stu[i].score=0;
		i++;
	}
	qsort(stu,N,sizeof(stu[0]),CmpName);//对开始输入此班级所有人数进行排序 这样在后面容易处理数据
	for(i=0;i<N;i++)
		stu1[i]=stu[i];                 //此变量是用来存放每一个人的姓名以及此人的各科考试的分数之和  开始时必须初始化为0
	scanf("%d",&M);
	//考试科目数 M   班级共有人数 N
	while(M--){
		for(i=0;i<N;i++){
			scanf("%d",&stu[i].score);  //输入每个同学的第i门科目分数
			gets(stu[i].name);
		}
		qsort(stu,N,sizeof(stu[0]),CmpName); //按学生姓名进行排序  这里是CmpName
		qsort(stu1,N,sizeof(stu1[0]),CmpName);
		for(i=0;i<N;i++){
			stu1[i].score+=stu[i].score;
			if(strcmp("Li Ming",stu1[i].name)==0)
				M_score=stu1[i].score;  //把Li Ming的总分单独拿出来
		}
		qsort(stu1,N,sizeof(stu1[0]),cmp);   //按学生总分进行排序  比较函数是cmp
		num=1;
		for(i=N-1;stu1[i].score>M_score;i--)
			if(stu1[i].score>M_score)
				num++;
		printf("%d\n",num);
	}
	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