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

MauricioC
New poster
Posts: 1
Joined: Sat Dec 16, 2006 10:26 pm
Location: Brazil

333 - WA

Post by MauricioC » Sat Dec 16, 2006 10:32 pm

I discovered this site last June, but only had the time to actually solve problems now, so I said, "let's start with an easy one, so I can get used to this thing". Picked problem 333.

That's when the fun started, of course. I missed the instructions for sending Java code, ans, as such, got a few CEs. I went there, read the docs, and tried again, only to get a WA. I have no idea what's wrong in this code... Several test cases obtained here in the forum work exactly the same. I even got some ISBNs from bookstores to try on my program, and it was fine.

Without further ado, here is the code. Can someone please give me a small hint?

Code: Select all

/* @JUDGE_ID: XXXXXXX 333 Java "Recognizing Good ISBNs" */ 

/*package e333;*/

import java.io.IOException;

class Main {
   public static void main(String[] args)
   {
	   String data;
	      
	   while ((data = ReadLn(80)) != null)
	   {	   
		   data = data.trim();
		   
		   if (calcularSoma(data) % 11 != 0)
		   {
			   System.out.println(data + " is incorrect. ");
		   }
		   else
		   {
			   System.out.println(data + " is correct. ");
		   }
	   }
   }
   
   static int calcularSoma(String data)
   {
	   int resultado = 0;
	   int resultado2 = 0;
	   int counter = 0;

	   for (int i = 0; i < data.length(); i++)
	   {
		   String caractere = String.valueOf(data.charAt(i));
		   if (data.charAt(i) >= '0' && data.charAt(i) <= '9')
	 	   {
		     resultado += Integer.parseInt(caractere);
		     resultado2 += resultado;
		     counter++;
		   }
		   else if (data.charAt(i) == 'X' && counter == 9)
		   {
			   resultado += 10;
			   resultado2 += resultado;
			   counter++;
		   }
		   else if (data.charAt(i) == '-')
		   {
		   }
		   else
		   {
			   return -1;
		   }
	   }
	   
	   if (counter == 10)
		   return resultado2;
	   else
		   return -1;
   }
   
   static String ReadLn (int maxLg)  // utility function to read from stdin
   {
       byte lin[] = new byte [maxLg];
       int lg = 0, car = -1;

       try
       {
           while (lg < maxLg)
           {
               car = System.in.read();
               if ((car < 0) || (car == '\n')) break;
               lin [lg++] += car;
           }
       }
       catch (IOException e)
       {
           return (null);
       }

       if ((car < 0) || (car == '\n') && (lg == 0)) return (null);  // eof
       return (new String (lin, 0, lg));
   }

}


User avatar
kana
New poster
Posts: 19
Joined: Mon Mar 13, 2006 6:03 pm
Location: dhaka

Post by kana » Sat Dec 23, 2006 9:56 pm

i've tried all the i/o from all exisiting threads on this problem. but i have failed to get AC. can any one help me to find whats the wrong with my code??? :(

Code: Select all

removed
Last edited by kana on Sat Oct 10, 2009 11:33 am, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Mon Dec 25, 2006 11:03 pm

Check the samples...

Input:

Code: Select all

0-89237-010 -6   
0-        89237-  010  - 6   
    0    -8923  7-  010-  6   
Output:

Code: Select all

0-89237-010 -6 is correct.
0-        89237-  010  - 6 is correct.
0    -8923  7-  010-  6 is correct.
Hope these help.
Ami ekhono shopno dekhi...
HomePage

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

333 PE

Post by abhi » Sun Apr 15, 2007 8:46 pm

why PE plz help

Code: Select all

# include <iostream>
# include <sstream>
# include <cstdio>
# include <string>

//# define debug(x) cout<<#x<<" - "<<x<<'\n';
using namespace std;

int ok(string s)
{
    int i;
    int s1[10]={0},s2=0;
    
    if(s.size()!=10) {/* debug(s.size())*/return 0;}
    
    for(i=0;i<s.size();i++)
        if(isalpha(s[i]))
        {
            if(i!=9)    return 0;
            if( s[i]!='X')
            return 0;
        }
            
    s1[0]=s[0]-'0';
   
    for(i=1;i<s.size()-1;i++)
        s1[i]+=s1[i-1]+(s[i]-'0');//debug(s1[i])
        
    if(s[9]=='X')   s1[9]=s1[8]+10;
    else s1[9]=s1[8]+s[9]-'0';
    
    //debug(s1[9]);
    
    for(i=0;i<=9;i++){
        s2+=s1[i];
      //  debug(s2)
    }
        
        if(s2%11 == 0)
            return 1;
            
         return 0;
}
    
    
int main()
{
    string str;
    char str1[10000];
    int i,flag=0;
    
    while(gets(str1))
    {
        if(flag == 1)
            cout<<'\n';
        
        int l=strlen(str1);
        for(i=0; str1[i]==' '|| str1[i]=='\t' ;i++);
        for(int j=i;j<l;j++)    str.push_back(str1[j]);
            
        flag = 1;
        string s;
               
        for(i=0;i<str.size();i++){
          if(isdigit(str[i])|| str[i]=='X')
                s+=str[i];
        }
          
        if(ok(s))
            cout<<str<<" is correct.";
        else cout<<str<<" is incorrect.";
    }
return 0;
}

        
        

abdullah<cse du>
New poster
Posts: 39
Joined: Mon Dec 04, 2006 2:18 pm
Location: Bangladesh(CSE DU)
Contact:

Post by abdullah<cse du> » Mon Apr 16, 2007 10:10 am

abhi,
In my accepted program i do not use flag option. So use 'endl' in every output and cut flag option. Another thing that if the given string has leading or trilling space avoid tham but space inside the string should not be avoided.

Input

Code: Select all

     125-458-478-0(space)
125-    458-   478-  0(no space)


Output

Code: Select all

125-458-478-0 is incorrect.
125-    458-   478-  0 is incorrect.

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

Post by abhi » Mon Apr 16, 2007 6:49 pm

thsi is my new code
still PE :(

Code: Select all


# include <iostream>
# include <sstream>
# include <cstdio>
# include <string>

//# define debug(x) cout<<#x<<" - "<<x<<'\n';
using namespace std;

int ok(string s)
{
    int i;
    int s1[10]={0},s2=0;
    
    if(s.size()!=10) {/* debug(s.size())*/return 0;}
    
    for(i=0;i<s.size();i++)
        if(isalpha(s[i]))
        {
            if(i!=9)    return 0;
            if( s[i]!='X')
            return 0;
        }
            
    s1[0]=s[0]-'0';
   
    for(i=1;i<s.size()-1;i++)
        s1[i]+=s1[i-1]+(s[i]-'0');//debug(s1[i])
        
    if(s[9]=='X')   s1[9]=s1[8]+10;
    else s1[9]=s1[8]+s[9]-'0';
    
    //debug(s1[9]);
    
    for(i=0;i<=9;i++){
        s2+=s1[i];
      //  debug(s2)
    }
        
        if(s2%11 == 0)
            return 1;
            
         return 0;
}
    
    
int main()
{
    string str;
    char str1[10000];
    int i/*,flag=0*/;
    
    while(gets(str1))
    {
       // if(flag == 1)
         //   cout<<'\n';
        
        str.clear();
        int l=strlen(str1);
        for(i=0; str1[i]==' '|| str1[i]=='\t' ;i++);
        for(int j=i;j<l;j++)    str.push_back(str1[j]);
        reverse(str.begin(),str.end());
        for(i=0;str[i]==' '||str[i]=='\t';i++);
        str.erase(str.begin(),str.begin()+i);
        reverse(str.begin(),str.end());
            
        //flag = 1;
        string s;
               
        for(i=0;i<str.size();i++){
          if(isdigit(str[i])|| str[i]=='X')
                s+=str[i];
        }
          
        if(ok(s))
            cout<<str<<" is correct."<<endl;
        else cout<<str<<" is incorrect."<<endl;
    }
return 0;
}

        
        

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

Post by abhi » Wed Apr 18, 2007 2:05 pm

i just dont understand the reason for PE.

please help :(

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Tue Apr 24, 2007 2:51 pm

I think you are getting compile error...
Ami ekhono shopno dekhi...
HomePage

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

Post by abhi » Wed Apr 25, 2007 2:27 pm

hi i am using Dev-cpp. it doesnt give any compile error on my machine.
may i know the reason for CE .

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Wed Apr 25, 2007 7:29 pm

Submit your code(The second one), you will get CE.
Ami ekhono shopno dekhi...
HomePage

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

Post by abhi » Thu Apr 26, 2007 4:31 pm

the second one does give error in the judge. but it compiles properly on my machine :(. may i know the reason.
and i also include all the necessary header files like <algorithm> etc ...
while making the final submission to the judge

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Sat Apr 28, 2007 10:16 pm

Turn your e-mail notifications on and then submit the code. You will be given a mail specifying the error.
Ami ekhono shopno dekhi...
HomePage

alamgir kabir
New poster
Posts: 37
Joined: Wed Oct 03, 2007 10:42 am

333 Please help me.

Post by alamgir kabir » Thu Nov 29, 2007 6:26 pm

Code: Select all

Code removed after acc

thomas1016
New poster
Posts: 19
Joined: Mon May 29, 2006 4:12 pm

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

Post by thomas1016 » Sun May 11, 2008 1:17 pm

why a blank line have such a output?
"is incorrect."

not " is incorrect."
(<space>is incorrect.)

Post Reply

Return to “Volume 3 (300-399)”