做C++ 的oj题时,老是出现runtime error。求大神解答。

代码如下
#include<iostream>
#include<cstring>
struct stu{
char name[100];
int l;
int num[100];
int sum;
};
int main()
{
using namespace std;
int n[100],m,g,v,t=0,i,j,len1,len2,k,b;
int sc[100],count[100];
for(i=0;i<100;i++)
count[i]=0;
stu ha[100][1000];
stu ho;
cin>>n[t];
while(n[t])
{
cin>>m>>g;
for(i=0;i<m;i++)
{
cin>>sc[i];
}
for(i=0;i<n[t];i++)
{
cin>>ha[t][i].name>>ha[t][i].l;
v=0;
for(j=0;j<ha[t][i].l;j++)
{
cin>>ha[t][i].num[j];
b=ha[t][i].num[j]-1;
v+=sc[b];
}
ha[t][i].sum=v;
if(v>=g)
count[t]++;
}
t++;
cin>>n[t];
}
for(i=0;i<t;i++)
{
for(k=0;k<n[i]-1;k++)
{
for(j=0;j<n[i]-1-k;j++)
{
len1=strlen(ha[i][j].name);
len2=strlen(ha[i][j+1].name);
if(ha[i][j].sum<ha[i][j+1].sum||(ha[i][j].sum==ha[i][j+1].sum&&ha[i][j].name[len1-1]>ha[i][j+1].name[len2-1]))
{
ho=ha[i][j];
ha[i][j]=ha[i][j+1];
ha[i][j+1]=ho;
}
}
}
for(j=0;j<count[i];j++)
cout<<ha[i][j].name<<' '<<ha[i][j].sum<<endl;
}
return 0;
}
题目:http://ac.jobdu.com/problem.php?pid=1014

楼主写得太麻烦啦没必要用二维数组 每场考试读入一组输出一组就行啦..贴代码已经ac了 注释也给你写啦
#include<iostream>
#include<algorithm>
using namespace std;
struct stu
{
string name;
int score;
};
bool cmp(stu a,stu b)
{
if(a.score>b.score) return true;
if(a.score==b.score&&a.name<b.name) return true;
return false;
}
int main()
{
int N,M,G,problemScore[10],problemNumber,cur,count;//problemScore[i]表示第i+1道题目的 //分数 problemNumber表示每个考生答题数 count表示过分数线人数
stu student[1000];//student储存每场考试的学生信息
while(cin>>N&&N)
{
count=0;
cin>>M>>G;
for(int i=0;i<M;i++) cin>>problemScore[i];
for(int i=0;i<N;i++)
{
cin>>student[i].name>>problemNumber;
student[i].score=0;
for(int j=0;j<problemNumber;j++)
{
cin>>cur; student[i].score+=problemScore[cur-1];
}
if(student[i].score>=G) count++;//计算高于分数线人数
}
sort(student,student+N,cmp);//按要求排序,将前count个输出即可
cout<<count<<endl;
for(int i=0;i<count;i++) cout<<student[i].name<<" "<<student[i].score<<endl;
}
return 0;
}追问

我用的是vc6。
你这代码在我的编辑器编译不了;
到oj上就ac了。
你是用什么编辑器?
还有可以的话能帮我看下为什么错吗?(初学者一个)

追答

我用的dev呀 可是vc不可能编译不过的呀 你编译器报错是什么呀
你这个错误就是ha数组开太大啦 100*1000太大放main函数里会re..你自己用编译器都过不了吧就去交题目..

改的方法吧..把那个数组放main函数外面当全局变量就可以啦 编译器可以正常运行 但是交题目会超内存
所以说多个测试案例的题目都不要存着最后输出 读入一组读出一组就好啦

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-02-26
只需把那几个数组给定义在全局定义域(main函数之前)就行了本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网