Page 9 of 12

333 - WA

Posted: Sat Dec 16, 2006 10:32 pm
by MauricioC
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));
   }

}

Posted: Sun Dec 17, 2006 4:57 am
by helloneo

Posted: Sat Dec 23, 2006 9:56 pm
by kana
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

Posted: Mon Dec 25, 2006 11:03 pm
by Jan
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.

333 PE

Posted: Sun Apr 15, 2007 8:46 pm
by abhi
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;
}

        
        

Posted: Mon Apr 16, 2007 10:10 am
by abdullah<cse du>
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.

Posted: Mon Apr 16, 2007 6:49 pm
by abhi
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;
}

        
        

Posted: Wed Apr 18, 2007 2:05 pm
by abhi
i just dont understand the reason for PE.

please help :(

Posted: Tue Apr 24, 2007 2:51 pm
by Jan
I think you are getting compile error...

Posted: Wed Apr 25, 2007 2:27 pm
by abhi
hi i am using Dev-cpp. it doesnt give any compile error on my machine.
may i know the reason for CE .

Posted: Wed Apr 25, 2007 7:29 pm
by Jan
Submit your code(The second one), you will get CE.

Posted: Thu Apr 26, 2007 4:31 pm
by abhi
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

Posted: Sat Apr 28, 2007 10:16 pm
by Jan
Turn your e-mail notifications on and then submit the code. You will be given a mail specifying the error.

333 Please help me.

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

Code: Select all

Code removed after acc

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

Posted: Sun May 11, 2008 1:17 pm
by thomas1016
why a blank line have such a output?
"is incorrect."

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