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

shovan.livebd
New poster
Posts: 1
Joined: Sat Jun 25, 2011 8:50 pm

Re: 333 - Recognizing Good ISBNs - Need more In/Output sampl

Post by shovan.livebd » Sat Jun 25, 2011 8:54 pm

Hello there. I am facing problem with my code. It is giving PE and I can't find the reason. Could anyone help?

Code is given below:

Code: Select all

# include <stdio.h>

int main()
{
	char isbn[81];
	int digits[10];
	int s1, s2;
	int scanned;
	int delspace;
	int count;
	int i, c;

	while(gets(isbn))
	{
		count=0;
		scanned=0;
		delspace=0;

		for(i=0; isbn[i]; i++)
		{
			if(scanned==0)
				if(isbn[i]==' ' || isbn[i]=='\t')
				{
					for(c=0; isbn[c]; c++)
						isbn[c]=isbn[c+1];
					i--;
				}
				else scanned++;

			if(scanned==1)
			{
				if(isbn[i]>='0' && isbn[i]<='9')
				{
					if(count<10) digits[count]=isbn[i]-'0';
					count++;
				}
				else if(isbn[i]=='X' && count==9)
				{
					digits[count]=10;
					count++;
				}
				else if(isbn[i]==' ' || isbn[i]=='\t')
				{
					delspace=i;
					scanned++;
				}
				else if(isbn[i]!='-') break;
				if(count>10) break;
			}

			if(scanned==2)
				if(isbn[i]!=' ' && isbn[i]!='\t')
				{
					delspace=0;
					break;
				}
		}
		if(delspace) isbn[delspace]='\0';
		if(isbn[0]=='\0')
		{
			printf("is incorrect.\n");
			continue;
		}
		if(count!=10 || isbn[i])
		{
			printf("%s is incorrect.\n", isbn);
			continue;
		}

		s1=0; s2=0;
		for(i=0; i<10; i++)
		{
			s1+=digits[i];
			s2+=s1;
		}
		if(s2%11) printf("%s is incorrect.\n", isbn);
		else      printf("%s is correct.\n", isbn);
	}
	return 0;
}

avatar
New poster
Posts: 8
Joined: Wed Nov 16, 2011 6:44 pm

333

Post by avatar » Wed Nov 16, 2011 7:12 pm

hellow.....someone please help me with problem 333
if there is any space inside the message .then what should i do..........
that is it correct or not????

avatar
New poster
Posts: 8
Joined: Wed Nov 16, 2011 6:44 pm

Re: 333 - Recognizing Good ISBNs - Need more In/Output sampl

Post by avatar » Wed Nov 16, 2011 7:18 pm

hellow .....
please help me..
if there are more than 10 digit but there summation satisfies the given condition ..then what should i do????

avatar
New poster
Posts: 8
Joined: Wed Nov 16, 2011 6:44 pm

Re: 333 - Recognizing Good ISBNs - Need more In/Output sampl

Post by avatar » Wed Nov 16, 2011 7:20 pm

hellow .....
please help me..
if there are more than 10 digit but there summation satisfies the given condition ..then what should i do????

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Location: Dhaka, Bangladesh
Contact:

Re: 333 - Recognizing Good ISBNs - Need more In/Output sampl

Post by uvasarker » Sat Jul 28, 2012 1:59 pm

Please, help me, I am getting W A continuously. Give me some hints or I/O.

Code: Select all

#include <cstdio>
#include <cstring>
#include <cctype>
int main()
{
		char in[500];
		//freopen("wtool.txt","r",stdin);
		//freopen("out-333.txt","w",stdout);
		while(gets(in))
		{
                int ln=strlen(in), ch=0, s1[500], ok=1, s2[500], total[500], len, hp=0, fag=0, f=0;
                len=ln;
				for(int i=0 ; i<ln ; i++)
				{
				    if( in[i]>='0' && in[i]<='9' )
				    {
				        s1[ch]=(int)in[i]-48;
				        ch++;
				    }
				    else if(in[i]=='X')
				    {
				        s1[ch]=10;
				        ch++;
				    }
				    else if(in[i]=='-' || in[i]==' ' )
				    {
				        ok=1;
				        if(in[i]=='-') hp++;
				    }
				}
                for(int i=0 ; i<ln ; i++){
				    if( (in[(ln-1)-i])==' ' && f==0 ) len--;
				    else f=1;
                }

				ln=len;
				s2[0]=s1[0];
				total[0]=s1[0];

				if(ch>0 )
				{
                        for(int i=1 ; i<ch ; i++)
                        {
                            s2[i]=s2[i-1]+s1[i];
                            total[i]=total[i-1]+s2[i];
                        }
                        for(int i=0 ; i<ln ; i++)
                        {
                            if(in[i]!=' ' && fag==0) fag=1;
                            if(  fag==1 ){
                                printf("%c",in[i]);
                            }
                        }
                        if(total[ch-1]%11==0) printf(" is correct.\n");
                        else printf(" is incorrect.\n");


				}
				else {
                        for(int i=0 ; i<len ; i++)
                        {
                            if(in[i]!=' ' && fag==0) fag=1;
                            if(  fag==1 ){
                                printf("%c",in[i]);
                            }
                        }
                        printf(" is incorrect.\n");
				}
		}

	return 0;
}

Anyone will explain please, why Uvatoolkit give these output:

Code: Select all

0 - 8923 7-010-6 is correct.
0-89237-10-6 is incorrect.
0-892...37-...010-6 is incorrect.
0-8()9237-010-6 is incorrect.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 333 - Recognizing Good ISBNs - Need more In/Output sampl

Post by brianfry713 » Mon Jul 30, 2012 11:44 pm

00000000X2 is incorrect.
Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Location: Dhaka, Bangladesh
Contact:

Re: 333 - Recognizing Good ISBNs - Need more In/Output sampl

Post by uvasarker » Tue Jul 31, 2012 8:54 am

Guru,
Still WA.

Code: Select all

#include <cstdio>
#include <cstring>
#include <cctype>
int main()
{
      char in[500];
      //freopen("wtool.txt","r",stdin);
      //freopen("out-333.txt","w",stdout);
      while(gets(in))
      {
                int ln=strlen(in), ch=0, s1[500], ok=1, s2[500], total[500], len, hp=0, fag=0, f=0, pos=0;
                len=ln;
            for(int i=0 ; i<ln ; i++)
            {
                if( in[i]>='0' && in[i]<='9' )
                {
                    s1[ch]=(int)in[i]-48;
                    ch++;
                }
                else if(in[i]=='X')
                {
                    s1[ch]=10;
                    if(pos==0) pos=ch;
                    ch++;
                }
                else if(in[i]=='-' || in[i]==' ' )
                {
                    ok=1;
                    if(in[i]=='-') hp++;
                }
            }
                for(int i=0 ; i<ln ; i++){
                if( (in[(ln-1)-i])==' ' && f==0 ) len--;
                else f=1;
                }

            ln=len;
            s2[0]=s1[0];
            total[0]=s1[0];

            if(ch==10 && pos==10 )
            {
                        for(int i=1 ; i<ch ; i++)
                        {
                            s2[i]=s2[i-1]+s1[i];
                            total[i]=total[i-1]+s2[i];
                        }
                        for(int i=0 ; i<ln ; i++)
                        {
                            if(in[i]!=' ' && fag==0) fag=1;
                            if(  fag==1 ){
                                printf("%c",in[i]);
                            }
                        }
                        if(total[ch-1]%11==0) printf(" is correct.\n");
                        else printf(" is incorrect.\n");


            }
            else {
                        for(int i=0 ; i<len ; i++)
                        {
                            if(in[i]!=' ' && fag==0) fag=1;
                            if(  fag==1 ){
                                printf("%c",in[i]);
                            }
                        }
                        printf(" is incorrect.\n");
            }
      }

   return 0;
}


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 333 - Recognizing Good ISBNs - Need more In/Output sampl

Post by brianfry713 » Wed Aug 01, 2012 3:18 am

Doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!

techbd123
New poster
Posts: 14
Joined: Tue Aug 06, 2013 3:42 pm

Re: 333 - Recognizing Good ISBNs - Need more In/Output sampl

Post by techbd123 » Wed Sep 18, 2013 6:59 pm

Guru! Brianfry!
Please help me. I used most of the critical cases but found WA. Please help
My code is here: http://ideone.com/AD8rfT

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 333 - Recognizing Good ISBNs - Need more In/Output sampl

Post by brianfry713 » Thu Sep 19, 2013 12:32 am

For your input:

Code: Select all

0-8104-5687-7
0-8104-5687-7432
This   just    for test.
0-1000-99999
0-123-4567-89
0-123-4567-890
1-111-111-111
0-89237-010-6
0-89237-010-6 TEST
XX-0000000000-XX
XX000000XXX0000XXXXX
1234567890
0823025713
013152447X
013152447x

082302571
08230257130
2803025713
07632200X1
 0823025713
0823025713
   0823025713
0823025713   
      0823025713      
082 3025713
0823025      713
--08-2----302--5713------
082302_5713
013152447X bla
013-A-152447X
0-1-3-1-5-2-4-4-7-X- -
01315
2447X
01-10
01-434435
01 0111 
0131524     47X
0_1_31524_47X
0131524 	47X
013152447X
0123456789X
My AC output:

Code: Select all

0-8104-5687-7 is correct.
0-8104-5687-7432 is incorrect.
This   just    for test. is incorrect.
0-1000-99999 is incorrect.
0-123-4567-89 is correct.
0-123-4567-890 is incorrect.
1-111-111-111 is correct.
0-89237-010-6 is correct.
0-89237-010-6 TEST is incorrect.
XX-0000000000-XX is incorrect.
XX000000XXX0000XXXXX is incorrect.
1234567890 is incorrect.
0823025713 is correct.
013152447X is correct.
013152447x is correct.
 is incorrect.
082302571 is incorrect.
08230257130 is incorrect.
2803025713 is incorrect.
07632200X1 is incorrect.
0823025713 is correct.
0823025713 is correct.
0823025713 is correct.
0823025713 is correct.
0823025713 is correct.
082 3025713 is incorrect.
0823025      713 is incorrect.
--08-2----302--5713------ is correct.
082302_5713 is incorrect.
013152447X bla is incorrect.
013-A-152447X is incorrect.
0-1-3-1-5-2-4-4-7-X- - is incorrect.
01315 is incorrect.
2447X is incorrect.
01-10 is incorrect.
01-434435 is incorrect.
01 0111 is incorrect.
0131524     47X is incorrect.
0_1_31524_47X is incorrect.
0131524 	47X is incorrect.
013152447X is correct.
0123456789X is incorrect.
Check input and AC output for thousands of problems on uDebug!

cyberdragon
New poster
Posts: 20
Joined: Fri Aug 30, 2013 5:42 am

333 - Recognizing Good ISBNs

Post by cyberdragon » Sun Mar 23, 2014 8:32 pm


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 333 - Recognizing Good ISBNs

Post by brianfry713 » Tue Mar 25, 2014 12:18 am

Try running your code on the sample input. Don't try to compile C++ code using a JAVA compiler.
Check input and AC output for thousands of problems on uDebug!

cyberdragon
New poster
Posts: 20
Joined: Fri Aug 30, 2013 5:42 am

Re: 333 - Recognizing Good ISBNs

Post by cyberdragon » Wed Mar 26, 2014 7:06 am

Well that was a bad mistake using getline.
But still don't know why WA after editing.

http://ideone.com/LkQpA5

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 333 - Recognizing Good ISBNs

Post by brianfry713 » Wed Mar 26, 2014 8:34 pm

00000000X2 is incorrect.
Check input and AC output for thousands of problems on uDebug!

cyberdragon
New poster
Posts: 20
Joined: Fri Aug 30, 2013 5:42 am

Re: 333 - Recognizing Good ISBNs

Post by cyberdragon » Thu Mar 27, 2014 4:14 am

In case of 00000000X2, s2 = 22 which is evenly divisible by 11.
Or does "evenly divisible" is different from "divisible" ?

Post Reply

Return to “Volume 3 (300-399)”