#include <stdio.h>
#define MAXL 400
struct book
{
long int w,v;
}book[MAXL];
long int strength=0,w_count,v_count=0;
void print()
{
printf("%ld",strength);
}
void sorting_book(int n)
{
int i;
for(i=0;i<n;i++)
{
if(i==0)
{
strength=strength+book[i].w;
w_count=w_count+book[i].w
v_count=v_count+book[i].v
}
else
{
strength=strength+w_count-v_count+book[i-1].w-book[i-1].v;
w_count=w_count+book[i-1].w;
v_count=v_count+book[i-1].v;
}
}
}
main()
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(i==0)
{
scanf("%ld",&book[i].w);
}
else
{
scanf("%ld %ld",&book[i].w,&book[i].v);
}
}
sorting_book(n);
print();
}
你算法有点问题,我上面的仅供参考,欢迎批评指教。