Kevin Chen
Kevin Chen

Reputation: 107

How to make Code more efficient/clean - USACO Training First Task

I'm having trouble with the first problem on the USACO Training Page.

The task is asking for two strings from a text.in file, converting the strings into a number that is the product of the letters (where a=1, b=2, z=26), then seeing if the remainders of the numbers/47 are equal to each other (if they are, print "GO", if not, print "STAY").

It works great on my computer, but when I send it in, it displays

Run 1: Execution error: Your program exited with exit status `1'.

    ------ Data for Run 1 [length=14 bytes] ------
    COMETQ 
    HVNGAT 
    ----------------------------

      Your program printed data to stderr.  Here is the data:
      -------------------
      Exception_in_thread_"main"_java.io.FileNotFoundException:_test.in_(No_such_file_or_directory)
        at_java.io.FileInputStream.open(Native_Method)
        at_java.io.FileInputStream.<init>(FileInputStream.java:106)
        at_java.io.FileInputStream.<init>(FileInputStream.java:66)
        at_java.io.FileReader.<init>(FileReader.java:41)
        at_ride.main(Unknown_Source)

I tried looking at this http://cerberus.delos.com:790/usacoprobfix?a=VjAAvKvQucH , but I couldn't really understand terms such as "stack usage" or "out-of-bounds".

Is the reason why it is not accepting my code because it is too slow? I would appreciate any help figuring out this problem.

  /*
ID: Anon
LANG: JAVA
TASK: ride
*/
import java.io.*;
import java.util.*;

class ride
{
  public static void main (String [] args) throws IOException
  {

     //input
     BufferedReader br = new BufferedReader(new FileReader("test.in"));

     //output
     PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out")));

     String nameComet = br.readLine();
     String nameGroup = br.readLine();

     int productComet = 1;
     int productGroup = 1;

     //loop through each letter in word
     for(int i=0; i<nameComet.length(); i++) 
     {
        //sets letter to char letter
        char letter = nameComet.charAt(i);
        //set number of letter to correspondnum
        int numComet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(letter) + 1;
        productComet *= numComet;
     }

     for(int i=0; i<nameGroup.length(); i++) 
     {
        //sets letter to char letter
        char letter = nameGroup.charAt(i);
        //set number of letter to correspondnum
        int numGroup = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(letter) + 1;
        productGroup *= numGroup;
     }

        int modComet = productComet % 47;
        int modGroup = productGroup % 47;

    if (modComet == modGroup)
    {
        out.println("GO");
    }
    else
    {
        out.println("STAY");
    }

     //close everything
     out.close();
     System.exit(0);
}
}

Upvotes: 2

Views: 1407

Answers (1)

Jlennon321
Jlennon321

Reputation: 228

Your error is on these lines:

BufferedReader br = new BufferedReader(new FileReader("test.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out")));

Your code is attempting to read from a file ("test.in") that doesn't exist on the usaco servers. When you submit your code to the website, you need to read from files that have the same name as that of the problem (e.g. "ride.in" and "ride.out").

Hope that helps!

Upvotes: 1

Related Questions