## 333 - Recognizing Good ISBNs

Moderator: Board moderators

NONAME_SUST
New poster
Posts: 8
Joined: Tue Jul 23, 2002 9:15 am

### GIVE ME SOME INPUT

Heloo,
I ahve solved the problem which is valid for all given inputs. But when I submit it I got wrong answer. Can anyone give me some crutial inputs to judge the problem???
HEY WANT TO GET SOL.TRY ON http://www.uvexam.zzn.com from 1st September

hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.

### 333 ISBN (**GOT WA~^_*...)

What's wrong in my source?

help me!!
thanks!

[c]
#include "stdio.h"
void print_it(char *s)
{
int i,j;
for(i=0;i<strlen(s);i++)
{
if( s!='\t' && s!=' ' )
break;
else
s=1;
}

for(i=strlen(s)-1;i>=0;i--)
{
if( s!='\t' && s!=' ' )
break;
else
s=1;
}
j=0;
for(i=0;i<strlen(s);i++)
{
if( s!=1&&s!='\t'&&s!=' ') {printf("%c",s);j=1;}

}
if(j==1) printf(" ");

}
void main()
{
char a;
int n,i;
int p,q,r;
while( gets(a) )
{
print_it(a);
n=0;
for(i=0;i<strlen(a);i++)
{
if( a[i]>='0'&&a[i]<='9' )
{
n++;
p[n-1]=a[i]-'0';
}
else if( a[i]=='X' && n==9 )
{
n++;
p[n-1]=10;
}
else if( a[i]!=' ' && a[i]!='-' && a[i]!='\t' )
{
printf("is incorrect.\n");
}
}

if( n!=10 ) {printf("is incorrect.\n");goto read_next;}

q=p;
for(i=1;i<10;i++)
q[i]=q[i-1]+p[i];
r=q;
for(i=1;i<10;i++)
r[i]=r[i-1]+q[i];
if(r!=0&&r%11==0) printf("is correct.\n");

}
}[/c]

New poster
Posts: 4
Joined: Wed Sep 18, 2002 2:16 am
Some little questions...

1. Hyphens can be anywere. (including two hyphens at the middle of the isbn number, like --0--1315--2447-X--???)

2. The ISBN cannot be less than 10 symbols long (of course, excluding hyphens) but can be more than 10 symbols. (what kind of symbols can i accept excluiding numbers, X's, hyphens and spaces???) (this isbn is correct: 0000000011053???)

3. There can be leading/trailing spaces and tabs. (ok!!!)

4. There can be only one 'X' and it can be only at last position in the ISBN (so, there can be hyphens after 'X'). (ok!!!)

thanks!

hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.
I got WA at this problem many times.
But I don't know why...
It is very strange. ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

### #333: Recognizing Good ISBNs - WA

Can anyone tell me why my program gets a WA?

I was so sure that I was correct!

[cpp]#include <stdio.h>
#include <string.h>

int main() {
bool valid;
char isbn;
int len, sum, sum2, i, j;

while (scanf("%s", &isbn) == 1) {
valid = true;
sum = sum2 = 0;
len = strlen(isbn);
for (i = 0, j = 0; i < len; i++) {
if (isbn >= '0' && isbn <= '9') {
sum += isbn - '0';
sum2 += sum;
j++;
}
else if (isbn == 'X') {
sum += 10;
sum2 += sum;
j++;
}
else if (isbn != '-') {
valid = false;
break;
}
}
if (valid)
valid = (j == 10 && sum2 % 11 == 0);

printf("%s is %s.\n", isbn, valid ? "correct" : "incorrect");
}

return 0;
}[/cpp]

Archer
New poster
Posts: 7
Joined: Tue Oct 22, 2002 11:51 pm

### 333 - ISBN format question

About the ISBN parser: What is it supposed to do if more than 10 digits appear in the input? Say "invalid ISBN"? or regard only the first 10 digits and proceed as normal?

Per
A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden
Input:
0-8104-5687-7
0-8104-5687-7432
Output:
0-8104-5687-7 is correct.
0-8104-5687-7432 is incorrect.
So more than 10 digits can never be a valid ISBN.

epsilon0
Experienced poster
Posts: 112
Joined: Tue Nov 12, 2002 11:15 pm
Location: Paris, France.

### you got one big problem

it looks correct, it seems correct, it tastes correct too, but it's not correct.

guess where the problem lies... hehe if you could i weren't writing this.

you have a pretty smart way to perform the check. but let this smartness not occult the flaws of your design.

000-000-071-X

this is a good code because 7*3 + 1*2 + 10 = 33 = 0 (% 11)

let's get to the heart of the "mystery" now:

000-000-00X-2

clearly 10*2 + 2 = 22 = 0 (% 11)

so your program would aknowledge such a code, although there is an 'X' where there should not be.

i guess your faith made you blind. once you decided your program was divine, you could not perceive the flaws anymore.

your program once modified would look like:

[c]if (code == X)
{
if (i != 10)
{
valid = FALSE;
break;
}
sum += 10;
sum2 += sum;
}[/c]

i don't recall all your variables names sorry about it BUT you get the idea. shouldn't be a big deal to handle now you've been enlightened.

epsilon0
Experienced poster
Posts: 112
Joined: Tue Nov 12, 2002 11:15 pm
Location: Paris, France.
no way.

an ISBN has EXACTLY ten "digits" (including the checking character in the end).

epsilon0
Experienced poster
Posts: 112
Joined: Tue Nov 12, 2002 11:15 pm
Location: Paris, France.
using 13,000 bytes of data memory to solve such a program, is wrong.

yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am
Dear ec3-limz you have used scanf to take input but input may be string just like: " This is right" . In this case your program may fail. So try to use gets(); I think this can help you.  supermin
New poster
Posts: 37
Joined: Sat Oct 12, 2002 9:54 am
Location: (Taiwan)
Contact:

I got AC(P.E.) with this problem 333.
But I can't find any wrong.I think the "(in)correct." is not the problem.

This is my thougt:

Ignore the spaces in front of the string,and output the followings.

Code: Select all

``````        char temp,prob,*p;
int i;

gets(temp);
i=0;
p=temp;
while(isspace(temp[i])) i++;
strcpy(prob,p+i);

printf("%s is (in)correct.\n",prob);
``````
ex:
01-10
01-434435
01 0111

I will output:

01-10 is (in)correct.
01-434435 is (in)correct.
01 0111 is (in)correct.

There is any wrong with my thought?

kurnia w
New poster
Posts: 18
Joined: Fri Dec 06, 2002 3:53 pm
Location: Indonesia
Contact:

### 333: WA, why?

Hi...can anybody help me what kind of input that make my code WA ??
0-89237-010-6 is correct, how about 0_89237_010_6 ??
anyway here is my code
[c]#include<stdio.h>
#include<string.h>

void main() {
char isbn;
int sum1,sum2,i,length,angka,max,flag;

while(scanf("%s",&isbn)==1) {
printf("%s",isbn);
length=strlen(isbn); max=0;
for(i=0; i<length; i++) {
if(max==10) { flag=0; break; }
if((isbn>=47 && isbn<=58) || isbn==45 || isbn==88) {
if(isbn=='X') angka[max++]=10;
if(isbn>=47 && isbn<=58) {
angka[max++]=isbn-'0';
flag=1;
}
}
else { flag=0; break; }
}
if(max==10 && flag==1) {
sum1=angka;
for(i=0; i<10; i++) {
sum1[i+1]=(sum1+angka[i+1]);
}
sum2=sum1;
for(i=0; i<10; i++) {
sum2[i+1]=(sum2+sum1[i+1]);
}
if(sum2%11==0) flag=1;
else flag=0;
}

if(flag==1 && max==10) printf(" is correct.\n");
else printf(" is incorrect.\n");
}
}
[/c]
Thank's....

yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am
By taking a glance to your program i see that you can't handle string input like " I am a boy".
Ouput should be I am a boy not correct
But i think your program will give I is incorrect.
Try to use gets instead of scanf.
Hope it helps.

kurnia w
New poster
Posts: 18
Joined: Fri Dec 06, 2002 3:53 pm
Location: Indonesia
Contact:
I'm already fixed it, but still got WA?? why ??