#include <stdio.h>
#include <time.h>
int main()
{
#ifdef LOCAL
freopen("date.in","r",stdin);
freopen("date.out", "w", stdout);
#endif
int u=0,v=0;
char c= getchar() ;
while (c!=EOF)
{
v=c=='O'? v+1:0;
u+=v;
}
printf("%d\n", u);
printf("Time used=%.2f\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
#include <time.h>
int main()
{
#ifdef LOCAL
freopen("date.in","r",stdin);
freopen("date.out", "w", stdout);
#endif
int u=0,v=0;
char c= getchar() ;
while (c!=EOF)
{
v=c=='O'? v+1:0;
u+=v;
}
printf("%d\n", u);
printf("Time used=%.2f\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
这段程序运行时间超过了3秒,而且还不清楚结果是否正确!
这是原题编号:UVa 1585 - Score
第一,把char改成int,因为EOF为-1,char只有正数。
第二,while中的条件改为(c=getchar())!=EOF,按照上面的写法,只能读取一个字符。而且是个死循环。因为getchar是一个字符一个字符输入的。
第三,改正上面一些问题之后,结果提示答案我依然不对,这个暂时未能找出原因。原因是因为没有循环!
第四,此程序运行时间过长,但是原因未能细究,期待以后补充!
#include <stdio.h>
#include <time.h>
int main()
{
int i;
scanf("%d", &i);
while (i--)
{
int c, u = 0, v = 0;
while ((c = getchar()) != EOF)
{
v = (c == 'O' ? v + 1 : 0);
u += v;
}
printf("%d\n", u);
}
printf("Time used=%.2f\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
#include <time.h>
int main()
{
int i;
scanf("%d", &i);
while (i--)
{
int c, u = 0, v = 0;
while ((c = getchar()) != EOF)
{
v = (c == 'O' ? v + 1 : 0);
u += v;
}
printf("%d\n", u);
}
printf("Time used=%.2f\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
这个程序运行正确!