494 - Kindergarten Counting Game

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
faisal neaz
New poster
Posts: 1
Joined: Mon Feb 24, 2003 10:10 am

494 Help me

Post 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]
turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

Your solution will crash if I give you a word whose length is, let's say, 500 characters.

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
razibcse
New poster
Posts: 50
Joined: Mon Jul 22, 2002 3:17 am
Location: SUST,BANGLADESH
Contact:

Increase ur array size

Post 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.
Zhao Le
Learning poster
Posts: 80
Joined: Mon May 05, 2003 4:09 am
Location: Shanghai,China

494 Why WA?

Post 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]
jai166
New poster
Posts: 10
Joined: Mon May 12, 2003 11:10 am

Why 494 WA? Could you help me?

Post 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]
Jalal
Learning poster
Posts: 65
Joined: Sun Jun 02, 2002 8:41 pm
Location: BANGLADESH
Contact:

Post 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;
}
jai166
New poster
Posts: 10
Joined: Mon May 12, 2003 11:10 am

I still don't understand!

Post by jai166 »

Sorry, I still don't understand. Could you explain it more clearly?
Jalal
Learning poster
Posts: 65
Joined: Sun Jun 02, 2002 8:41 pm
Location: BANGLADESH
Contact:

Post 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++;
}
User avatar
ezra
New poster
Posts: 31
Joined: Thu Nov 21, 2002 2:11 pm

Post by ezra »

try this input
...as...df..
..sd.dff.gg

the answer should be
2
3

good luck!
jai166
New poster
Posts: 10
Joined: Mon May 12, 2003 11:10 am

Thanks, I've solved it!

Post 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]
User avatar
Riyad
Experienced poster
Posts: 131
Joined: Thu Aug 14, 2003 10:23 pm
Location: BUET
Contact:

494 wa ,plizzzzzzzzzzz help

Post 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
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post 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.
User avatar
Riyad
Experienced poster
Posts: 131
Joined: Thu Aug 14, 2003 10:23 pm
Location: BUET
Contact:

thanx a lot

Post 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:
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN
problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

494 run time error

Post 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]
Frostina
New poster
Posts: 23
Joined: Mon Dec 15, 2003 5:21 am

Post by Frostina »

Try this ... ^^"

[c]
char t[1000000];
[/c]
Thanks for your help ! ;)
Post Reply

Return to “Volume 4 (400-499)”