如何加快程序运行速度

xberlino xberlino 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这是usaco的第一道题,提示运行时间过长,求解各位
下面是我的代码
#include<stdio.h>
#include<string.h>
#define max 10
struct person
{
char name[17];
int money;
};
typedef struct person person;
person Person[max];
int num;
//look up the name
person * lookup(char *a)
{
char i;
for(i=0;i<max;i++)
{
if(strcmp(a,Person[i].name)==0)
return &Person[i];
}
return &Person[0];
}

main()
{
FILE *fin=fopen("gift1.in","r");
FILE *fout=fopen("gift1.out","w");
fscanf(fin,"%d",&num);
//print in all the name
int i,j;
for(i=0;i<num;i++)
{
fscanf(fin,"%s",Person[i].name);
Person[i].money=0;
}
//Do with the giving and receiving
char name[17];
person *giv,*rec;
int sum,n;
for(i=0;i<num;i++)
{
fscanf(fin,"%s,%d,%d",name,&sum,&n);
if(n!=0)
{
giv=lookup(name);
(* giv).money+=sum%n;
for(j=0;j<n;j++)
{
fscanf(fin,"%s",name);
rec=lookup(name);
(* rec).money+=sum/n;
}
}
}
//show the result
for(i=0;i<num;i++)
{
fprintf(fout,"%s %d\n",Person[i].name,Person[i].money);
}

exit(0);
}
...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
xberlino
C语言 70,039 成员 243,237 内容