如何加快程序运行速度

xberlino 2012-12-09 05:47:14
这是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);
}
...全文
365 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xberlino 2012-12-14
  • 打赏
  • 举报
回复
引用 3 楼 ForestDB 的回复:
也许算法逻辑得改。
后来改成了c++的就过了,不解了。下面是代码 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ofstream fout ("gift1.out"); ifstream fin ("gift1.in"); int num; fin>>num; string name[num]; int a[num]; for(int i=0;i<num;i++) a[i]=0; for(int i=0; i<num; i++) fin>>name[i]; int money, divnum; string person; for(int i=0; i<num ; i++) { fin>>person; fin>>money; fin>>divnum; for(int j=0; j<num; j++) if(name[j]==person&&divnum!=0) a[j]-=(money/divnum)*divnum; for(int i=0; i<divnum; i++) { fin>>person; for(int i=0; i<num; i++) if(name[i]==person) a[i]+=(money/divnum); } } for(int i=0; i<num; i++) fout<<name[i]<<' '<<a[i]<<endl; return 0; }
xberlino 2012-12-14
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
查MSDN是Windows程序员必须掌握的技能之一。 mk:@MSITStore:c:\MSDN98\98VS\2052\vccore.chm::/HTML/perfhm.htm Performance Tuning This section of the documentation talks about tuning an application throug……
这。。。。我没看懂是啥意思
xberlino 2012-12-14
  • 打赏
  • 举报
回复
引用 1 楼 xiaoqin515515 的回复:
给出具体题目吧
http://ace.delos.com/usacoprob2?a=tZV6RqbXd29&S=gift1 这是题目的链接
Binzo 2012-12-12
  • 打赏
  • 举报
回复
换更好的电脑。
赵4老师 2012-12-12
  • 打赏
  • 举报
回复
查MSDN是Windows程序员必须掌握的技能之一。 mk:@MSITStore:c:\MSDN98\98VS\2052\vccore.chm::/HTML/perfhm.htm Performance Tuning This section of the documentation talks about tuning an application through code improvements and profiling. Feature Only in Professional and Enterprise Editions Profiling is supported only in Visual C++ Professional and Enterprise Editions. For more information, seeVisual C++ Editions. Overviews How Do I Details -------------------------------------------------------------------------------- What else do you want to do? Enable profiling in the development environment. Select code to profile. Interesting home pages Visual C++ Home The entire Visual C++ product.
wayz11 2012-12-12
  • 打赏
  • 举报
回复
循环嵌套的太多,效率必然很慢,尤其是数据量很大时。不知道原题,不太好改。 但至少你的lookup函数很慢,用哈希吧,会快很多
乔巴好萌 2012-12-12
  • 打赏
  • 举报
回复
你这个是提交后自动测试的? 类似于acm on line的? 不清楚后台测试时 是如何对你程序进行测试的 如果不是依赖于fscanf 可能你程序就堵在那里了 提示超时 有提示具体时间吗
ForestDB 2012-12-11
  • 打赏
  • 举报
回复
也许算法逻辑得改。
一起来玩玩呗 2012-12-10
  • 打赏
  • 举报
回复
引用 1 楼 xiaoqin515515 的回复:
给出具体题目吧
+1
慧钦 2012-12-09
  • 打赏
  • 举报
回复
给出具体题目吧

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧