333 - Recognizing Good ISBNs
Moderator: Board moderators
-
- 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???
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
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[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]
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]
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!
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!
#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[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]
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]
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?
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.
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.
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.
about P.E.
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.
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?
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);
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?
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[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....
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....