该学习了。。。
#include<stdio.h>
#include<stdlib.h>
struct farmer
{
long ai; //amount of milk
long pi; //price
};
long f(struct farmer *p,long n,long m)
{
long cost=0,num=0,i,j,k;
struct farmer t;
for(i=0;i<m-1;i++) //sort
{
k=i;
for(j=i+1;j<m;j++)
if(p[j].pi<p[k].pi)
k=j;
t=p[k];
p[k]=p[i];
p[i]=t;
}
for(i=0;i<m;i++)
{
if(num+p[i].ai<n)
{
num+=p[i].ai;
cost+=p[i].ai*p[i].pi;
}
else
{
cost+=(n-num)*p[i].pi;
return cost;
}
}
return -1L;
}
int main()
{
long n,m,i; //n,is the amount of milk that Merry Milk Makers' want per day.
//m,is the number of farmers that they may buy from.
struct farmer *p;
scanf("%ld%ld",&n,&m);
p=(struct farmer*)malloc(m*sizeof(struct farmer));
for(i=0;i<m;i++)
scanf("%ld%ld",&(p+i)->pi,&(p+i)->ai);
printf("%ld\n",f(p,n,m));
free(p);
return 0;
}
[此贴子已经被作者于2006-11-3 20:39:30编辑过]
那你把//这样的注释全去掉,这是c++注释方式
#include<stdio.h>
#include<stdlib.h>
struct farmer
{
long ai;
long pi;
};
long f(struct farmer *p,long n,long m)
{
long cost=0,num=0,i,j,k;
struct farmer t;
for(i=0;i<m-1;i++)
{
k=i;
for(j=i+1;j<m;j++)
if(p[j].pi<p[k].pi)
k=j;
t=p[k];
p[k]=p[i];
p[i]=t;
}
for(i=0;i<m;i++)
{
if(num+p[i].ai<n)
{
num+=p[i].ai;
cost+=p[i].ai*p[i].pi;
}
else
{
cost+=(n-num)*p[i].pi;
return cost;
}
}
return -1L;
}
int main()
{
long n,m,i;
struct farmer *p;
scanf("%ld%ld",&n,&m);
p=(struct farmer*)malloc(m*sizeof(struct farmer));
for(i=0;i<m;i++)
scanf("%ld%ld",&(p+i)->pi,&(p+i)->ai);
printf("%ld\n",f(p,n,m));
free(p);
return 0;
}