π&γ 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 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?