读书人

uvaoj中10055,494总是answer wrong

发布时间: 2013-01-02 13:08:44 作者: rapoo

uvaoj中10055,494老是answer wrong
如题所示,10055,494提交总是answer wrong,而不是AC。程序在VC以及gcc都是可以得到正确结果的。现在把代码贴出来,请各位大牛帮帮忙,看看问题出在哪里。
10055代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void hashmat(void)
{
long a,b,ret;
while(scanf("%d%d",&a,&b) == 2)
{
ret = (a-b);
printf("%d\n",ret);
}
}
int main(void)
{
hashmat();
return 0;
}

494代码如下:
char buf[100];
int counting_game(void)
{
FILE *fp = NULL;
int length,i,count=0,len;
char *p,*q,*m;

fp = fopen("counting_game.in","r");
if(!fp)
{
printf("can not open file!\n");
return -1;
}
while(!feof(fp))
{
memset(buf,0,101);
count=0;
if(fgets(buf,101,fp) != NULL)
{
//memset(buf,0,101);
len = strlen(buf);
m = (char *)malloc(len);
memset(m,0,len);
strcpy(m,buf);
m[len-1]='\0';
p = strtok(m," ");
q = p;
while(p)
{
length = strlen(q);
for(i=0;i<length-1;i++)
{
if((q[i]>='a' && q[i]<='z') || (q[i]>='A' && q[i]<='Z'))
continue;
else
break;
}
if(i == length-1)
count++;
p = strtok(NULL," ");
q = p;
}
free(m);
printf("%d\n",count);
}
}
fclose(fp);
return 0;
}
int main(void)
{
if(counting_game() != 0)
printf("failed!\n");
return 0;
}
其中文件counting_game.in里的内容如下:
Meep Meep!
I tot I taw a putty tat.
I did! I did! I did taw a putty tat.
Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...
494主要是统计每一行的单词数目。
[解决办法]
10055,long只能表示到2^31-1。题目会大到2^32。
494不要文件io。不要多输出什么failed, cannot open file这种信息。judge程序以为你程序的答案是那个。
m[len-1]='\0'; 你这句把一行最后一个字母去掉了。如果最后是以单字母结尾的话你就少个词了。
for(i=0;i<length-1;i++) 也就是说我假设单独一个逗号两边都是空格,你会算这是一个词?

读书人网 >软件架构设计

热点推荐