On the moral and 1015, C

The first finished only 16 points, improved by the discovery that too many inappropriate array definition, as well as ordering method, resulting in lack of time to run, and ultimately only 19 points, the first time the code is not deleted, on the last plane the

The second code reference to the solution of a problem predecessors, and was modified, it seems easy to some of the older generation with the structure and function qsort, I forgot you can use the structure, so it looks more comfortable
(seniors Code)
this is a solution out of the question:

#include <stdio.h>
#include <stdlib.h>

typedef struct{
	int id;
	int de;
	int cai;
}student;

int fun(const void *a1,const void *a2)
{
	student * b1 = (student *) a1;
	student * b2 = (student *) a2;
	if( (b1->de+b1->cai) > (b2->de+b2->cai) )		//这里用b1.de不行,必须用b1->de,不清楚原因
		return -1;
	else if( (b1->de+b1->cai) < (b2->de+b2->cai) )
		return 1;
	else				//这里德才分相加相等
	{
		if( b1->de > b2->de )	return -1;		//比较德分
		else if( b1->de < b2->de )	return 1;
		else				//德分也相等,比较学号
		{
			if( b1->id > b2->id )	return 1;
			else return -1;
		}
	}
}

int main()
{
	int x,y,z;
	student a[100000]={0},b[100000]={0},c[100000]={0},d[100000]={0}; 
//注意这个定义数组也可以放在scanf("%d%d%d",&n,&low,&you);语句后面,然后用student a[n],b[n],c[n],d[n];这样提交的答案
//也是正确的,但是我用的vc++2010编译就不成功,这种方式更节省空间
	int n,low,you,sum=0;
	int j1=0,j2=0,j3=0,j4=0;

	scanf("%d%d%d",&n,&low,&you);	//学生个数,及格分,优等分
	while(n)
	{
		scanf("%d %d %d",&x,&y,&z);		//每一次输入成绩都依次放在对应的组别中,不及格的数据不存入
		if(y>=low && z>=low)
		{
			if( y>=you && z>=you )	{a[j1].id=x; a[j1].de=y; a[j1].cai=z; j1++;}
			else if( y>=you )		{b[j2].id=x; b[j2].de=y; b[j2].cai=z; j2++;}
			else if( y>=z )			{c[j3].id=x; c[j3].de=y; c[j3].cai=z; j3++;}
			else					{d[j4].id=x; d[j4].de=y; d[j4].cai=z; j4++;}
		}
		n--;
	}

	qsort(a, j1, sizeof(a[0]), fun);	//qsort排序,在进行大量数据排序的时候速度较快一些,使用规则见总结首页
	qsort(b, j2, sizeof(b[0]), fun);
	qsort(c, j3, sizeof(c[0]), fun);
	qsort(d, j4, sizeof(d[0]), fun);
	
	sum = j1+j2+j3+j4;
	printf("%d\n",sum);
	
	n=0;
	while(n<j1)
	{
		printf("%d %d %d\n",a[n].id,a[n].de,a[n].cai);
		n++;
	}
	n=0;
	while(n<j2)
	{
		printf("%d %d %d\n",b[n].id,b[n].de,b[n].cai);
		n++;
	}
	n=0;
	while(n<j3)
	{
		printf("%d %d %d\n",c[n].id,c[n].de,c[n].cai);
		n++;
	}
	n=0;
	while(n<j4)
	{
		printf("%d %d %d",d[n].id,d[n].de,d[n].cai);
		n++;
        if( n!=j4 )  printf("\n");
	}

	return 0;
}

The following is what I had written, not with the structure and qsort sorting, a maximum of 19 points

#include <stdio.h>

int paixu(int* a, int* aa,int* aaa,int n)
{
	int i=0,j=0,k=0;
	for(i=0; i<n-1; i++)
		for(j=i+1; j<n; j++)
		{
			if( (aa[i]+aaa[i])<(aa[j]+aaa[j]) ){
			k = aa[i];    aa[i] = aa[j];     aa[j] = k;
			k = aaa[i];   aaa[i] = aaa[j];   aaa[j] = k;
			k = a[i];	  a[i] = a[j];       a[j] = k;	}

			else if((aa[i]+aaa[i])==(aa[j]+aaa[j]))
			{
				if(aa[i] < aa[j]){							//总分并列,按德分排列
					k = aa[i];    aa[i] = aa[j];     aa[j] = k;
					k = aaa[i];   aaa[i] = aaa[j];   aaa[j] = k;
					k = a[i];	  a[i] = a[j];       a[j] = k;	}
				else if(aa[i] == aa[j] && a[i] > a[j]){		 //总分和德分都并列且学号从大到小,需换位
					k = aa[i];    aa[i] = aa[j];     aa[j] = k;
					k = aaa[i];   aaa[i] = aaa[j];   aaa[j] = k;
					k = a[i];	  a[i] = a[j];       a[j] = k;	}
			}
		}	
	return 0;
}

int main()
{
	int a[100000]={0},aa[100000]={0},aaa[100000]={0},b[100000]={0},bb[100000]={0},bbb[100000]={0},c[100000]={0},
	    cc[100000]={0},ccc[100000]={0},d[100000]={0},dd[100000]={0},ddd[100000]={0};
	int x[100000]={0},xx[100000]={0},xxx[100000]={0};
	int i=0,n=1,low,you,sum=0;
	int j=0,jj=0,jjj=0,jjjj=0;

	scanf("%d%d%d",&n,&low,&you);
	while(n)
	{
		scanf("%d%d%d",&x[i],&xx[i],&xxx[i]);
		if(xx[i]>=low && xxx[i]>=low)
		{
			if( xx[i]>=you && xxx[i]>=you )	{a[j]=x[i];aa[j]=xx[i];aaa[j]=xxx[i];j++;}
			else if( xx[i]>=you )			{b[jj]=x[i];bb[jj]=xx[i];bbb[jj]=xxx[i];jj++;}
			else if( xx[i]>=xxx[i] )		{c[jjj]=x[i];cc[jjj]=xx[i];ccc[jjj]=xxx[i];jjj++;}
			else							{d[jjjj]=x[i];dd[jjjj]=xx[i];ddd[jjjj]=xxx[i];jjjj++;}
		}
		n--;i++;
	}

	n=0;	while(a[n])	 n++;	sum += n;	paixu(a,aa,aaa,n);
	n=0;	while(b[n])	 n++;	sum += n;	paixu(b,bb,bbb,n);
	n=0;	while(c[n])	 n++;	sum += n;	paixu(c,cc,ccc,n);
	n=0;	while(d[n])	 n++;	sum += n;	paixu(d,dd,ddd,n);
	printf("%d\n",sum);
	
	n=0;
	while(a[n])
	{
		printf("%d %d %d\n",a[n],aa[n],aaa[n]);
		n++;
	}
	n=0;
	while(b[n])
	{
		printf("%d %d %d\n",b[n],bb[n],bbb[n]);
		n++;
	}
	n=0;
	while(c[n])
	{
		printf("%d %d %d\n",c[n],cc[n],ccc[n]);
		n++;
	}
	n=0;
	while(d[n])
	{
		printf("%d %d %d",d[n],dd[n],ddd[n]);
		n++;
        if( d[n] )  printf("\n");
	}

	return 0;
}
Published 44 original articles · won praise 0 · Views 885

Guess you like

Origin blog.csdn.net/weixin_43916400/article/details/104382237