333 - Recognizing Good ISBNs

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

Moderator: Board moderators

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

Well, I've found more reasonable error. In convert() you cannot do

Code: Select all

char * aux = strcpy(aux,&c);
You have to allocate memory for aux. And passing &c as parameter in this case may be risky. You don't know where the NULL character is in the memory.
Schutzstaffel
New poster
Posts: 37
Joined: Fri Apr 30, 2004 6:52 pm
Location: Portugal

Post by Schutzstaffel »

That doesn't make sense for some reasons:

1 - there is always a char '\0' since strcpy puts it and also the function trim takes care of that.

2-

Code: Select all

0823025713
if it's this line with no trailing or leading spaces that crashes because it has exactly lenght 10 then why didn't it crash sooner since

Code: Select all

1234567890
comes first and also hash lenght 10?

3- assuming that's the problem then if I change the condition in the loop of verifyChars to :

Code: Select all

unsigned int size =strlen(line);
  while (i != size+1)
  {
it should be ok but it gives RE anyway. :?
Did I miss something?
Thanks again.

EDIT: just checked your last post and you're right, it must be that! Going to try that now, thanks once again
Image
Schutzstaffel
New poster
Posts: 37
Joined: Fri Apr 30, 2004 6:52 pm
Location: Portugal

Post by Schutzstaffel »

Yeah it was the missing malloc :P
Thanks a lot, got AC.
Code edited.

Edit: just a note, I've just realised I didn't need any strcpy afterall, I can use the reference to the char directly in function atoi isntead of a copy of the char in a char * (really dumb of me to only notice this now :oops: )
Image
Wei-Ming Chen
Experienced poster
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

333 strange.........

Post by Wei-Ming Chen »

I got WA, why?

#include <stdio.h>
#include <string.h>
int main()
{
char x[100],y;
int a,b,c,d,e,f[15],g,h;
while(gets(x))
{
a=strlen(x);
c=0;
d=0;
g=-1;
for(b=0;b<a;b++)
{
if(x[b]==' ')
{
if(d==0)
{
g=b;
}
else if(d!=0 && d!=10)
{
c=1;
break;
}
}
else if(x[b]=='-')
{
continue;
}
else if(x[b]=='X')
{
if(d==9)
{
d++;
f[d]=10;
}
else
{
c=1;
break;
}
}
else if(48<=x[b]<=57)
{
if(d<10)
{
d++;
f[d]=x[b]-48;
}
else
{
c=1;
break;
}
}
else
{
c=1;
break;
}
}
for(d=2;d<=10;d++)
{
f[d]+=f[d-1];
}
for(d=2;d<=10;d++)
{
f[d]+=f[d-1];
}
if(f[10]%11!=0)
{
c=1;
}
if(g==a-1)
{
printf(" is incorrect.\n");
continue;
}
for(h=a-1;h>=0;h--)
{
if(x[h]!=' ')
{
break;
}
}
for(d=g+1;d<=h;d++)
{
printf("%c",x[d]);
}
printf(" is ");
if(c==1)
{
printf("in");
}
printf("correct.\n");
}
return 0;
}
mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

Have a look at http://online-judge.uva.es/board/viewto ... 0253#40253 8)
OK, I'll repost.
Try to pass the following inputs

Code: Select all

0-89237-010-6
0-89237-010 -6
0-1-3-1-6-2-9-5-9-X
0abc-13dfasdfa-162959-X
    0-13-162959-X    	 // leading/trailing spaces
0-13-162959-X 0-13-162959-X
0-13-162959-X1
-------0-89237-010-6--------
0-89237-010-6XXXX
0-89237-010-6-150
				            // blank line
0-89237-010- 6 TEST
Output should be

Code: Select all

0-89237-010-6 is correct.
0-89237-010 -6 is correct.
0-1-3-1-6-2-9-5-9-X is correct.
0abc-13dfasdfa-162959-X is correct.
0-13-162959-X is correct.
0-13-162959-X 0-13-162959-X is incorrect.
0-13-162959-X1 is incorrect.
-------0-89237-010-6-------- is correct.
0-89237-010-6XXXX is incorrect.
0-89237-010-6-150 is incorrect.
 is incorrect.
0-89237-010- 6 TEST is incorrect.
Wei-Ming Chen
Experienced poster
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

Post by Wei-Ming Chen »

I know you have posted.
When I tried, another person told me that the second input could be correct or incorrect.
And I didn't know why the input code is correct.....
By the way, I changed the code and used these inputs, and they were right.
But when I sumbit, WA again.
mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

Did you look at other threads regarding this? Look at http://online-judge.uva.es/board/viewtopic.php?t=9650. Some more hints may also be present. Search!
Wei-Ming Chen
Experienced poster
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

Post by Wei-Ming Chen »

Can you tell me why the fourth input is correct?
I thought it is incorrect.
IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am

Post by IRA »

why the fourth input is correct?
???
mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

IRA & Wei-Ming Chen wrote:why the fourth input is correct?
???
According to my implementation, valid ISBN may contain illegal characters like small letters but not capital except X at 10th position.
Wei-Ming Chen
Experienced poster
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

Post by Wei-Ming Chen »

I went to see that page and changed my code.
But WA......
I'm confused,I didn't think my code wrong.

#include <stdio.h>
#include <string.h>
int main()
{
char x[100],y;
int a,b,c,d,e,f[15],g,h;
while(gets(x))
{
a=strlen(x);
c=0;
d=0;
g=-1;
for(b=0;b<a;b++)
{
if(x[b]==' ')
{
if(d==0)
{
g=b;
}
}
else if(x[b]=='X')
{
if(d==9)
{
d++;
f[d]=10;
}
else
{
c=1;
break;
}
}
else if(48<=x[b] && x[b]<=57)
{
if(d<10)
{
d++;
f[d]=x[b]-48;
}
else
{
c=1;
break;
}
}
else if(x[b]>=65 && x[b]<=90)
{
c=1;
break;
}
else
{
continue;
}
}
for(d=2;d<=10;d++)
{
f[d]+=f[d-1];
}
for(d=2;d<=10;d++)
{
f[d]+=f[d-1];
}
if(f[10]%11!=0)
{
c=1;
}
if(g==a-1)
{
printf(" is incorrect.\n");
continue;
}
for(h=a-1;h>=0;h--)
{
if(x[h]!=' ')
{
break;
}
}
for(d=g+1;d<=h;d++)
{
printf("%c",x[d]);
}
printf(" is ");
if(c==1)
{
printf("in");
}
printf("correct.\n");
}
return 0;
}
totobogy
New poster
Posts: 7
Joined: Wed Jan 11, 2006 10:08 pm
Location: India
Contact:

333-WA?

Post by totobogy »

Hi. Plz help with this code. I get a WA but I tested all the cases available.

Code: Select all

#include<stdio.h>

int isdigit(char c)
{
	switch(c)
 	{
 	case '0': return 0;
  	case '1': return 1;
    	case '2': return 2;	
     	case '3': return 3;	
      	case '4': return 4;	
       	case '5': return 5;	
       	case '6': return 6;	
       	case '7': return 7;	
       	case '8': return 8;	
       	case '9': return 9;
       	//case 'x': return 10;
       	case 'X': return 10;
        default: return -1;
        
     }
}		
	
int main()
{
	int i=0,n;   
	char c;
	int s1 =0, s2 =0;
	
	while((c=getchar())!=EOF)/*till EOF*/
	{
		i=0;
		s1=0;
		s2=0;
		while(c!='\n' && c!=EOF)/*new ISBN*/
		{
			if(c != ' ')/*Leave out the spaces, print everything else*/
				printf("%c",c);	
		
		    if((n=isdigit(c))!= -1) /*If character is a digit*/
		    {
		    	i++; /*count digits*/
		    	s1 += n; /*Partial Sums*/
		    	s2 += s1;
			}
		
		    c = getchar();
		
	    }
	  
	    if(i==10 && s2%11 == 0)
	    	printf(" is correct.\n");
	  
 		else /*If i><10 or s2%11 != 0*/
	  		printf(" is incorrect.\n");

    }
	return 0;
}
		
Erik
Learning poster
Posts: 67
Joined: Fri Jul 01, 2005 11:29 am
Location: Germany
Contact:

Post by Erik »

Hi,

what about the following case:

Code: Select all

0000-0000-X2
Cu, Erik :D
silver
New poster
Posts: 1
Joined: Thu Sep 21, 2006 10:28 am

333.....WA

Post by silver »

i am a novice programmer....i am trying uva problem 333....but it is WA :( :(
anyone plzz help me....
here is my code

Code: Select all

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int main()
{
	char str[81],str1[80],str2[80];
	int x,p,flag,i,fal,S[80],S2[80],a,l1,l2,arr[80],N,N1,N2,arr2[80];
	while(gets(str))
	{
		l1=strlen(str);
		if(l1==0)flag=0;
		fal=0;
		a=0;
		p=0;
		x=0;
		for(i=0;i<l1;i++)
		{
			
			if(a==10 && str[i]=='X' && x!=1)
			{
				arr[a]=10;
				x=1;
				flag=1;
			}
			if(isdigit(str[i])>0)
				{
					
					arr[a]=(str[i]-'0');
					
					a++;
					
					flag=1;
				}
				
			if(isspace(str[i])==0)
			{str1[p]=str[i];p++;}	 	 
			else fal++;
		}
		str1[p]='\0';
		if(fal==(l1-1))flag=0;
		 
		if(a>10)flag==0;
		if(flag==1)
		{
		S[0]=arr[0];
		S2[0]=S[0];
		for(i=1;i<a;i++)
		{
			S[i]=S[i-1]+arr[i];
			
			
			S2[i]=S2[i-1]+S[i];
		}
		
		sprintf(str2,"%d",S2[i]);
		l2=strlen(str2);
		N1=0;
		for(i=0;i<l2;i++)
		{
			arr2[i]=(str2[i]-'0');
		}
		for(i=0;i<l2;i+=2)
		{
			N1=N1+arr2[i];
		}
		N2=0;
		for(i=1;i<l2;i+=2)
		{
			N2=N2+arr2[i];
		}
		
		N=0;
		N=abs(N1-N2);
		if((N%11)==0)
		printf("%s is correct.\n",str1);
		else if((N%11)!=0){printf("%s is incorrect.\n",str1);}
		}
		else if(flag==0){printf("%s is incorrect.\n",str1);}
	
	}
	return 0;
}
Sumon
New poster
Posts: 17
Joined: Fri May 30, 2003 8:14 pm
Location: Bangladesh
Contact:

Re: 333 - PE - but why?

Post by Sumon »

Karthekeyan wrote:Here's my code that gives presentation error with oj.... is it because of the endl on seeing eof??
I think you have printed the trailing spaces of the input string and getting PE.

In general,the leading and trailing spaces must be deleted when print the input string as output.

Warning: No,spaces inside the string should be deleted.
Change your view,your life will be changed.
Post Reply

Return to “Volume 3 (300-399)”