早教吧作业答案频道 -->其他-->
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...的网友还看了以下:
有关电路的一道二阶微分方程LCU''+LU'/R+U=C求U关于时间T的函数U是函数L,C,R,C 2020-04-26 …
急性肺水肿时可闻及A:两肺底湿啰音B:局限性干啰音C:双肺满布湿啰音D:局限性湿啰音E:双肺满布 2020-06-07 …
支气管扩张时可闻及A:双肺满布干啰音B:局限性湿啰音C:局限性干啰音D:两肺底湿啰音E:双肺满布 2020-06-07 …
左心衰竭患者不可能出现的体征是A:心率增快B:双肺底湿啰音C:哮鸣音D:双肺满布湿啰音E:三凹征 2020-06-07 …
∫dx/xlnxlnlnx(1)∫dx/[xlnxln(lnx)]=∫d(lnx)/[lnxln( 2020-08-01 …
以下哪项对RNA来说是正确的()A.(G+C)=(A+U)B.(G+A)=(C+U)C.(C+G)> 2020-11-06 …
请在下列句子中选出字形有错误的一项A.荒谬筹码诀窍迄今银幕银屏/荧屏B.勾联螺旋意蕴牵涉提炼啰唆/啰 2020-11-10 …
不定积分初学者求助∫1/2x+1dx令u=2x+1,则du=2dx得∫1/2x+1dx=1/2∫du 2020-11-15 …
请在下列句子中选出字形有错误的一项A.荒谬筹码诀窍迄今银幕银屏/荧屏B.勾联螺旋意蕴牵涉提炼啰唆/啰 2020-12-15 …
为什么判断影响平行板电容器的因素时,保持Q和S不变,d越大,偏转角度越小?保持S不变,d越大,依据公 2020-12-19 …