π&γ 2021-01-31 22:23 采纳率: 100%
浏览 83
已采纳

大佬们,请问PTA 1015德才论我编的C语言代码应该怎么改?感谢!感谢!

题目如下:

 

PTA上提交结果的显示是这样的:

 

 

我的代码:

#include <stdio.h>
#include <string.h>
struct student{
	char id[9];
	int moral;
	int intell;
	int total;
}; 
void sort(struct student*arr,int n);
void swap(struct student*a,struct student*b);
int main()
{
	int N,L,H;
	scanf("%d%d%d",&N,&L,&H);
	struct student stu[100];
	int i=0;
	int cnt=0;
	for(i=0;i<N;i++){
		scanf("%s %d %d",stu[i].id,&stu[i].moral,&stu[i].intell);
		stu[i].total=stu[i].intell+stu[i].moral;
	}
	struct student A[N];
	struct student B[N];
	struct student C[N];
	struct student D[N];
	int cnt1=0;
	int cnt2=0;
	int cnt3=0;
	int cnt4=0;
	for(i=0;i<N;i++){
		if(stu[i].moral>=L&&stu[i].intell>=L){
			cnt++;
			if(stu[i].moral>=H&&stu[i].intell>=H){
				A[cnt1++]=stu[i];
			}
			else if(stu[i].intell<H&&stu[i].moral>=H){
				B[cnt2++]=stu[i];
			}
			else if(stu[i].moral<H){
				if(stu[i].moral>stu[i].intell) C[cnt3++]=stu[i];
				else D[cnt4++]=stu[i];
			}
			
		}
	}
	printf("%d\n",cnt);
	sort(A,cnt1);
	sort(B,cnt2);
	sort(C,cnt3);
	sort(D,cnt4);
	return 0;
}
void sort(struct student*arr,int n)
{
	int i,j;
	struct student tmp;
	for(i=0;i<n-1;i++){
		for(j=0;j<n-i-1;j++){
			if(arr[j].total<arr[j+1].total) swap(&arr[j],&arr[j+1]);
			else if(arr[j].total==arr[j+1].total){
				if(arr[j].moral<arr[j+1].moral) swap(&arr[j],&arr[j+1]);
				else if(arr[j].moral==arr[j+1].moral){
					if(strcmp(arr[j].id,arr[j+1].id)>0) swap(&arr[j],&arr[j+1]);
					}
			}
		}
	} 
	for(i=0;i<n;i++){
		printf("%s %d %d\n",arr[i].id,arr[i].moral,arr[i].intell);
	}
}
void swap(struct student*a,struct student*b)
{
	struct student tmp=*a;
	*a=*b;
	*b=tmp;
}

感谢!

  • 写回答

6条回答 默认 最新

  • pencefarmer 2021-01-31 22:40
    关注

    数组出现了栈溢出的情况,可能是系统栈区的内存不够用,所以提问者,需要将struct的单元内存大小进行缩减

     

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作