HDU ACM problem 1236 为什么会Runtime Error (ACCESS_VIOLATION)?以下是小弟写的代码,涉世不深,若啰嗦请见谅在本机上运行貌似也没错了,为什么会Runtime Error (ACCESS_VIOLATION)?这个一般是什么原因引起
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/28 12:15:19
HDU ACM problem 1236 为什么会Runtime Error (ACCESS_VIOLATION)?以下是小弟写的代码,涉世不深,若啰嗦请见谅在本机上运行貌似也没错了,为什么会Runtime Error (ACCESS_VIOLATION)?这个一般是什么原因引起
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);
}
}
}
HDU ACM problem 1236 为什么会Runtime Error (ACCESS_VIOLATION)?以下是小弟写的代码,涉世不深,若啰嗦请见谅在本机上运行貌似也没错了,为什么会Runtime Error (ACCESS_VIOLATION)?这个一般是什么原因引起
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