杭电ACM 1031 Runtime Error
原题:http://acm.hdu.edu.cn/showproblem.php?pid=1031ProblemDescription.Herearewhatheobta...
原题:http://acm.hdu.edu.cn/showproblem.php?pid=1031
Problem Description
. Here are what he obtained: N people voted for M design elements (such as the ACM-ICPC logo, big names in computer science, well-known graphs, etc.). Everyone assigned each element a number of satisfaction. However, XKA can only put K (<=M) elements into his design. He needs you to pick for him the K elements such that the total number of satisfaction is maximized.
自己写的代码如下:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
//double element[1002];
double a[1002],b[1002],c[1002];
int d[1002];
typedef struct element
{
int id;
double satisfaction;
}element;
element e[1002];
int cmp1( const void *x , const void *y )
{
struct element *p = (element *)x;
struct element *q = (element *)y;
if(p->satisfaction!= q->satisfaction)
return p->satisfaction < q->satisfaction ? 1:-1;
else
return p->id > q->id? 1:-1;
}
int cmp2(const void *x , const void *y )
{
return *(int *)x< *(int*)y ?1:-1;
}
int main(int argc, char* argv[])
{
int n,m,k;
int i;
while(scanf("%d%d%d",&n,&m,&k)==3)
{
for(i=0;i<m;++i)
{
scanf("%lf",&a[i]);
e[i].id=i;
e[i].satisfaction=a[i];
}
for(i=0;i<m;++i)
{
scanf("%lf",&b[i]);
e[i].satisfaction+=b[i];
}
for(i=0;i<m;++i)
{
scanf("%lf",&c[i]);
e[i].satisfaction+=c[i];
}
qsort(e,m,sizeof(e[0]),cmp1);
for(i=0;i<k;++i)
d[i]=e[i].id;
qsort(d,k,sizeof(d[0]),cmp2);
for(i=0;i<k-1;++i)
printf("%d ",d[i]+1);
printf("%d\n",d[i]+1);
// memset(d,0,k*sizeof(*d));
// memset(e,0,m*sizeof(element));
}
return 0;
}
在VC6.0上编译通过,运行好像没问题。。可是提交时是
Runtime Error
(ACCESS_VIOLATION)不知道哪里错了。。。
麻烦看到的帮忙看一下代码,想知道哪里错了,直接贴AC代码的就不必了。。。。谢谢啦。。。。。 展开
Problem Description
. Here are what he obtained: N people voted for M design elements (such as the ACM-ICPC logo, big names in computer science, well-known graphs, etc.). Everyone assigned each element a number of satisfaction. However, XKA can only put K (<=M) elements into his design. He needs you to pick for him the K elements such that the total number of satisfaction is maximized.
自己写的代码如下:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
//double element[1002];
double a[1002],b[1002],c[1002];
int d[1002];
typedef struct element
{
int id;
double satisfaction;
}element;
element e[1002];
int cmp1( const void *x , const void *y )
{
struct element *p = (element *)x;
struct element *q = (element *)y;
if(p->satisfaction!= q->satisfaction)
return p->satisfaction < q->satisfaction ? 1:-1;
else
return p->id > q->id? 1:-1;
}
int cmp2(const void *x , const void *y )
{
return *(int *)x< *(int*)y ?1:-1;
}
int main(int argc, char* argv[])
{
int n,m,k;
int i;
while(scanf("%d%d%d",&n,&m,&k)==3)
{
for(i=0;i<m;++i)
{
scanf("%lf",&a[i]);
e[i].id=i;
e[i].satisfaction=a[i];
}
for(i=0;i<m;++i)
{
scanf("%lf",&b[i]);
e[i].satisfaction+=b[i];
}
for(i=0;i<m;++i)
{
scanf("%lf",&c[i]);
e[i].satisfaction+=c[i];
}
qsort(e,m,sizeof(e[0]),cmp1);
for(i=0;i<k;++i)
d[i]=e[i].id;
qsort(d,k,sizeof(d[0]),cmp2);
for(i=0;i<k-1;++i)
printf("%d ",d[i]+1);
printf("%d\n",d[i]+1);
// memset(d,0,k*sizeof(*d));
// memset(e,0,m*sizeof(element));
}
return 0;
}
在VC6.0上编译通过,运行好像没问题。。可是提交时是
Runtime Error
(ACCESS_VIOLATION)不知道哪里错了。。。
麻烦看到的帮忙看一下代码,想知道哪里错了,直接贴AC代码的就不必了。。。。谢谢啦。。。。。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询