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

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

GIVE ME SOME INPUT

Post by NONAME_SUST »

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~^_*...)

Post by hank »

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[1000];
int n,i;
int p[1000],q[1000],r[1000];
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");
goto read_next;
}
}

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

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

read_next:
}
}[/c]
nicopadu
New poster
Posts: 4
Joined: Wed Sep 18, 2002 2:16 am

Post by nicopadu »

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.

Post by hank »

I got WA at this problem many times.
But I don't know why...
It is very strange.
:cry:
ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

#333: Recognizing Good ISBNs - WA

Post by ec3_limz »

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[81];
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

Post by Archer »

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

Post by Per »

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

Post by epsilon0 »

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.

think about it:

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.

Post by epsilon0 »

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.

Post by epsilon0 »

using 13,000 bytes of data memory to solve such a program, is wrong.
User avatar
yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am

Post by yahoo »

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. :lol: :lol:
supermin
New poster
Posts: 37
Joined: Sat Oct 12, 2002 9:54 am
Location: (Taiwan)
Contact:

about P.E.

Post by supermin »

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[100],prob[100],*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?

Post by kurnia w »

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[81];
int sum1[10],sum2[10],i,length,angka[10],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[0]=angka[0];
for(i=0; i<10; i++) {
sum1[i+1]=(sum1+angka[i+1]);
}
sum2[0]=sum1[0];
for(i=0; i<10; i++) {
sum2[i+1]=(sum2+sum1[i+1]);
}
if(sum2[9]%11==0) flag=1;
else flag=0;
}

if(flag==1 && max==10) printf(" is correct.\n");
else printf(" is incorrect.\n");
}
}
[/c]
Thank's....
User avatar
yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am

Post by yahoo »

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:

Post by kurnia w »

I'm already fixed it, but still got WA?? why ??
Post Reply

Return to “Volume 3 (300-399)”