早教吧作业答案频道 -->其他-->
HDUACMproblem1236为什么会RuntimeError(ACCESSVIOLATION)?以下是小弟写的代码,涉世不深,若啰嗦请见谅在本机上运行貌似也没错了,为什么会RuntimeError(ACCESSVIOLATION)?这个一般是什么原因引起
题目详情
HDU ACM problem 1236 为什么会Runtime Error (ACCESS_VIOLATION)?
以下是小弟写的代码,涉世不深,若啰嗦请见谅
在本机上运行貌似也没错了,为什么会Runtime Error (ACCESS_VIOLATION)?
这个一般是什么原因引起的呢?
#include
#include
#include
struct stu
{
char id[21];
int solved;
int num[5];
int score;
};
int judge(char a[20],char b[20])
{
int length_a;
int length_b;
length_a = strlen(a);
length_b = strlen(b);
if(length_a < length_b)
return 1;
else if(length_a > length_b)
return 2;
else
{
for(int i=0; i b[i])
{
return 2;
break;
}
}
}
};
void main()
{
int men;
int num;
int passline;
while(scanf("%d",&men)!=EOF)
{
if(men == 0)
break;
scanf("%d %d",&num,&passline);
int section[10];
struct stu *student[1000];
for(int i=0; iscore = 0;
scanf("%d",&student[j]->solved);
for(int k=0; ksolved; k++)
{
scanf("%d",&student[j]->num[k]);
student[j]->score+=section[student[j]->num[k]-1];
}
}
int count=0;
struct stu *temp;
temp = (stu *)malloc(sizeof(struct stu));
for(int l=0; lscore >= passline)
{
student[count] = temp;
count++;
}
}
for(int x=1; xscore)
{
temp = student[y-1];
student[y-1] = student[y];
student[y] = temp;
}
else if(student[y-1]->score == student[y]->score)
{
if(judge(student[y-1]->id ,student[y]->id) == 2)
{
temp = student[y-1];
student[y-1] = student[y];
student[y] = temp;
}
}
}
}
printf("%d\n",count);
for(int z=0; zid,student[z]->score);
}
}
}
以下是小弟写的代码,涉世不深,若啰嗦请见谅
在本机上运行貌似也没错了,为什么会Runtime Error (ACCESS_VIOLATION)?
这个一般是什么原因引起的呢?
#include
#include
#include
struct stu
{
char id[21];
int solved;
int num[5];
int score;
};
int judge(char a[20],char b[20])
{
int length_a;
int length_b;
length_a = strlen(a);
length_b = strlen(b);
if(length_a < length_b)
return 1;
else if(length_a > length_b)
return 2;
else
{
for(int i=0; i b[i])
{
return 2;
break;
}
}
}
};
void main()
{
int men;
int num;
int passline;
while(scanf("%d",&men)!=EOF)
{
if(men == 0)
break;
scanf("%d %d",&num,&passline);
int section[10];
struct stu *student[1000];
for(int i=0; iscore = 0;
scanf("%d",&student[j]->solved);
for(int k=0; ksolved; k++)
{
scanf("%d",&student[j]->num[k]);
student[j]->score+=section[student[j]->num[k]-1];
}
}
int count=0;
struct stu *temp;
temp = (stu *)malloc(sizeof(struct stu));
for(int l=0; lscore >= passline)
{
student[count] = temp;
count++;
}
}
for(int x=1; xscore)
{
temp = student[y-1];
student[y-1] = student[y];
student[y] = temp;
}
else if(student[y-1]->score == student[y]->score)
{
if(judge(student[y-1]->id ,student[y]->id) == 2)
{
temp = student[y-1];
student[y-1] = student[y];
student[y] = temp;
}
}
}
}
printf("%d\n",count);
for(int z=0; zid,student[z]->score);
}
}
}
▼优质解答
答案和解析
Runtime Error (ACCESS_VIOLATION)原因可能是:
(1)int num[5]不够,当一个学生解决的题目总数>5时越界;
(2)malloc的student没有free
另外:
(1)函数judge可能有问题,用strcmp代替;
(2)为保证正确free,要交换student的数据而不是指针.
以下修改AC:
#include
#include
#include
struct stu
{
char id[21];
int solved;
int num[10];//int num[5];
int score;
};
int judge(char a[20],char b[20])
{
int length_a;
int length_b;
length_a = strlen(a);
length_b = strlen(b);
if(length_a < length_b)
return 1;
else if(length_a > length_b)
return 2;
else
{
for(int i=0; i b[i])
{
return 2;
break;
}
}
}
};
void main()
{
int men;
int num;
int passline;
while(scanf("%d",&men)!=EOF)
{
if(men == 0)
break;
scanf("%d %d",&num,&passline);
int section[10];
struct stu *student[1000];
for(int i=0; iscore = 0;
scanf("%d",&student[j]->solved);
for(int k=0; ksolved; k++)
{
scanf("%d",&student[j]->num[k]);
student[j]->score+=section[student[j]->num[k]-1];
}
}
int count=0;
struct stu *temp;
//temp = (stu *)malloc(sizeof(struct stu));
for(int l=0; lscore >= passline)
{
*student[count] = *temp;//student[count] = temp;
count++;
}
}
struct stu temp2;//added
for(int x=1; xscore)
{
temp2 = *student[y-1];//temp = student[y-1];
*student[y-1] = *student[y];//student[y-1] = student[y];
*student[y] = temp2;//student[y] = temp;
}
else if(student[y-1]->score == student[y]->score)
{
//if(judge(student[y-1]->id ,student[y]->id) == 2)
if(strcmp(student[y-1]->id ,student[y]->id) > 0)
{
temp2 = *student[y-1];//temp = student[y-1];
*student[y-1] = *student[y];//student[y-1] = student[y];
*student[y] = temp2;//student[y] = temp;
}
}
}
}
printf("%d\n",count);
for(int z=0; zid,student[z]->score);
}
for(int k=0; k
(1)int num[5]不够,当一个学生解决的题目总数>5时越界;
(2)malloc的student没有free
另外:
(1)函数judge可能有问题,用strcmp代替;
(2)为保证正确free,要交换student的数据而不是指针.
以下修改AC:
#include
#include
#include
struct stu
{
char id[21];
int solved;
int num[10];//int num[5];
int score;
};
int judge(char a[20],char b[20])
{
int length_a;
int length_b;
length_a = strlen(a);
length_b = strlen(b);
if(length_a < length_b)
return 1;
else if(length_a > length_b)
return 2;
else
{
for(int i=0; i b[i])
{
return 2;
break;
}
}
}
};
void main()
{
int men;
int num;
int passline;
while(scanf("%d",&men)!=EOF)
{
if(men == 0)
break;
scanf("%d %d",&num,&passline);
int section[10];
struct stu *student[1000];
for(int i=0; iscore = 0;
scanf("%d",&student[j]->solved);
for(int k=0; ksolved; k++)
{
scanf("%d",&student[j]->num[k]);
student[j]->score+=section[student[j]->num[k]-1];
}
}
int count=0;
struct stu *temp;
//temp = (stu *)malloc(sizeof(struct stu));
for(int l=0; lscore >= passline)
{
*student[count] = *temp;//student[count] = temp;
count++;
}
}
struct stu temp2;//added
for(int x=1; xscore)
{
temp2 = *student[y-1];//temp = student[y-1];
*student[y-1] = *student[y];//student[y-1] = student[y];
*student[y] = temp2;//student[y] = temp;
}
else if(student[y-1]->score == student[y]->score)
{
//if(judge(student[y-1]->id ,student[y]->id) == 2)
if(strcmp(student[y-1]->id ,student[y]->id) > 0)
{
temp2 = *student[y-1];//temp = student[y-1];
*student[y-1] = *student[y];//student[y-1] = student[y];
*student[y] = temp2;//student[y] = temp;
}
}
}
}
printf("%d\n",count);
for(int z=0; zid,student[z]->score);
}
for(int k=0; k
看了 HDUACMproblem1...的网友还看了以下:
地球距太阳设为R,已知引力常数G,假如地球静止,太阳绕地球转,角速度w与地球绕太阳转的角速度相同( 2020-05-13 …
什么时候或者什么卫星是可以用mg=mV^2/r有一种情况是可以mg=mV^2/r=GMm/R^2( 2020-06-05 …
当分子间相距为r时,分子引力与斥力相等.当分子间距离小于r时,斥斥力起主要作用,当分子间距离大于r 2020-06-16 …
发射宇宙飞船的过程要克服引力做功,已知将质量为m的飞船在距地球中心无限远处移到距地球中心为r处的过 2020-07-08 …
点电荷AB各带电荷量9Q和-Q相距为r.点电荷A、B两点在同一水平电场线上,A在右,A.B各带电荷 2020-07-12 …
第二宇宙速度又叫做逃逸速度.太阳的半径为R,太阳的逃逸速度为c500,其中c为光在真空中传播的速度 2020-07-30 …
分子力与分子间距关系的那个图,请告诉我是怎么出来的,怎么也弄不明白.当r>r0时(r=ro时,引力= 2020-11-03 …
引力常数除以半径的平方是什么u/r^2u为引力常数 2020-12-01 …
在有机合成中,常利用反应:R-CN+2H2O+H+-→R-COOH+NH4+引入羧基,而-CN可以通 2020-12-28 …
万有引力作用下的物体具有引力势能,取无穷远处引力势能为零,物体距星球球心距离为r时的引力势能为:Ep 2021-01-09 …