Page 1 of 8

494 Help me

Posted: Mon Feb 24, 2003 10:20 am
by faisal neaz
[cpp]
why did I get a runtime error?

/* @begin_of_source_code */
/* @JUDGE_ID: 27962FX 494 C++ */
#include<stdio.h>
#include<string.h>
int main()
{
char sen[100];
int a=0,b,count=0;
while(scanf("%c",&sen[a])!=EOF)
{
if(sen[a]=='\n')
{
a=0;
do
{
if(sen[a]==' '||!(sen>='a'&&sen<='z'||sen>='A'&&sen<='Z'))
{
b=a;
while(sen!='\n')
{
if((sen>='a'&&sen<='z'||sen>='A'&&sen<='Z')&&(sen[b-1]==' '))
{
++count;
break;
}
++b;
}
a=b;
}
++a;
}while(sen[a]!='\n');
printf("%d\n",count+1);
count=0;
a=0;
}
++a;
}
return 0;
}
/* @end_of_source_code */


[/cpp]

Posted: Mon Feb 24, 2003 11:12 am
by turuthok
Your solution will crash if I give you a word whose length is, let's say, 500 characters.

-turuthok-

Increase ur array size

Posted: Thu Feb 27, 2003 6:13 am
by razibcse
I think, you are new in this business...
Always try to use bigger arrays...that will use some extra memory, but your program will never get RTE...

in this case, you should use an array size of 1,000,000...
if it doesn't work, try decreasing a little....

Razib

Do or do not, there is no try.

494 Why WA?

Posted: Sat May 10, 2003 4:11 am
by Zhao Le
i don't understand.
i also tried many times, who can help me?
i shall be grateful.
[cpp]#include <iostream.h>
#define Max 5000

char S[Max];

void main()
{
while(cin.getline(S,Max))
{
int length=cin.gcount()-1; // delete the return key
int count=0,i=0;
while(i<length)
{
while(S==' ') // Chop off ' ' before character
i++;
while(S!=' ')
{
if(i==length) break;
if(S>='a'&&S<='z'||S>='A'&&S<='Z')
i++;
else break;
}
if(S==' '||i==length) count++; // Indeed the word case including last case
while(S!=' ')
{
if(i==length) break;
i++;
}

}
cout<<count<<endl;
}
}[/cpp]

Why 494 WA? Could you help me?

Posted: Sat May 17, 2003 10:54 am
by jai166
Sorry, but I don't understand why it is wrong!!!
Could you help me? Thank you very much! :cry:

[c]#include<stdio.h>
#include<string.h>
int count(char str[])
{
register int sum=0,i;
register char *p;

p=strtok(str," ");

do{
for(i=0;*(p+i)!=NULL;i++)
if((*(p+i)>96&&*(p+i)<123)||(*(p+i)>64&&*(p+i)<91)){
sum++;
break;
}
p=strtok(NULL," ");
}while(p);
return sum;
}
void main(void)
{
char str[255];
while(gets(str))
printf("%d\n",count(str));
}[/c]

Posted: Sun May 18, 2003 2:01 am
by Jalal
U shouldnt break the loop in the if statement run untill any new line entered and count sum.......... :)
that all...............

if((*(p+i)>96&&*(p+i)<123)||(*(p+i)>64&&*(p+i)<91))
{
sum++;
break;
}

I still don't understand!

Posted: Mon May 19, 2003 11:14 am
by jai166
Sorry, I still don't understand. Could you explain it more clearly?

Posted: Mon May 26, 2003 11:56 am
by Jalal
actually u dont need to break counting the sum.......
It means write in the following way......

if((*(p+i)>96&&*(p+i)<123)||(*(p+i)>64&&*(p+i)<91))
{
sum++;
}

Posted: Tue Jun 17, 2003 4:02 pm
by ezra
try this input
...as...df..
..sd.dff.gg

the answer should be
2
3

good luck!

Thanks, I've solved it!

Posted: Thu Jul 31, 2003 11:26 am
by jai166
Thanks, I've solved it! :D
[c]#include<stdio.h>
#include<string.h>
void main(void)
{
char p[255];
register int sum,i;
while(gets(p)){
for(sum=0,i=0;*(p+i)!=NULL;i++)
if(((*(p+i)>96&&*(p+i)<123)||(*(p+i)>64&&*(p+i)<91))&&(*(p+i+1)<65||*(p+i+i)>122||(*(p+i+i)>90&&*(p+i+i)<97)))
sum++;
printf("%d\n",sum);
}
}[/c]

494 wa ,plizzzzzzzzzzz help

Posted: Tue Aug 19, 2003 11:00 am
by Riyad
i cant understand what is wrong in my code. i am having trouble understanding the input style. is this a valid input:
.........as.........asdf..........???????????............qwer
.................aaaaaa....................bbbbbbbbbbbbb..

if these input format is right what modification in my code should be done.plizzzzzzzzzzz help .here is my code:

[c] #include<stdio.h>

#define size 100000


int main(){

char input[size],ch;
register int i,flag,fch;
int count;

while(gets(input)){
count=0;
flag=1,fch=1;

for(i=0;input!='\0';i++){

ch=input;
if(ch==32){

if(flag==0&& fch==1){

count++;
flag=1;
}



}
else if(ch!=32){


if(flag==1){
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
fch=1;
flag=0;
}
else{

fch=0;
}

}



}


}



if(flag==0&&fch==1)
count+=1;

printf("%d\n",count);
}

return 0;


}

[/c]


thanx is advance .looking for help.
Riyad

Posted: Tue Aug 19, 2003 3:12 pm
by UFP2161
Yes, they are valid input. Reread this line from the problem:
A "word'' is defined as a consecutive sequence of letters (upper and/or lower case).
This does not necessarily mean that they must be separated by a space, just anything that is a non-letter. Thus your inputs above should return 3 and 2 respectively.

thanx a lot

Posted: Tue Aug 19, 2003 5:20 pm
by Riyad
thanx a lot friend . got it ac . i could not understand the input format . got it now. thanx once again. :D
bye
Riyad :) :lol:

494 run time error

Posted: Tue Nov 25, 2003 11:45 am
by problem
why run time error.i m frustrated.plz help me

[c]


#include<stdio.h>
#include<ctype.h>
main()
{
char t[12000];
char c,*x;
int i=0,check=0,count=0;
while((c=getchar())!=EOF)
{
t[i++]=c;
if(c=='\n')
{
t='\0';
x=t;
while(*x)
{
while(isalpha(*x))
{
if(*x=='\0')
break;
check=1;
x++;
}
while(isdigit(*x))
{
if(*x=='\0')
break;
check=1;
x++;
}
if(isspace(*x))
{
if(check==1)
{
count++;
check=0;
}
x++;
}
if(!isalpha(*x))
{
if(*x=='\n'||*x=='\0')
{
printf("%d\n",count);
count=0;
check=0;
x++;
break;
}
}
}
i=0;
}
}
}
[\c]

Posted: Mon Dec 15, 2003 1:29 pm
by Frostina
Try this ... ^^"

[c]
char t[1000000];
[/c]