Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: problem !!

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question problem !!

    hi ... i'm starting to solve usaco problems and i'm a beginner....
    so the problem is easy .... or hard i dont know

    for the first program (friday 13th) it gives me an output of array with zeros which i tried to debug with (print) to see the variables' changes (as i learned here) and my conclusion is that the loop (for) doesn't work !! and this is the part that i can't solve really .....
    for the second program the greedy gift giver it gives an empty output.....
    thx

    package friday;
     
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.*;
     
    public class friday {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)throws IOException {
     
            int[] array = new int[7] ;
            int ini = 3 ;
            int  a=0 , b , c  , d , e , f ,  g , h ;
            int  n , i ;
            Scanner s = new Scanner(new File("friday.in"));
            PrintWriter pw = new PrintWriter(new File("friday.out"));
            n = s.nextInt() ;
            for (i=0 ; i == n-1 ; i++ )
            {
            if ((i%4)==0)
            {
            a= ((ini+5) % 7) ;
            b = ((ini+9)%7) ;
            c = ((ini+4)%7) ;
            d = ((ini+1)%7) ;
            e = ((ini+7)%7) ;
            f= ((ini+3)%7) ;
            array[a] = array[a]+1 ;
            array[b] = array[b]+1 ;
            array[c] = array[c]+4;
            array[d] = array[d]+1 ;
            array[e] = array[e]+4 ;
            array[f] = array[f]+1 ;
            ini = (ini + 6)%7   ;
     
            }
     else
            a= ((ini+5) % 7) ;
            b = ((ini+9)%7) ;
            c = ((ini+4)%7) ;
            d = ((ini+1)%7) ;
            e = ((ini+7)%7) ;
            f= ((ini+3)%7) ;
            g = ((ini+6)%7) ;
            h = ((ini+2)%7) ;
            array[a] = array[a]+1  ;
            array[b] = array[b]+1 ;
            array[f] = array[f]+2  ;
            array[e] = array[e]+3  ;
            array[g] = array[g]+4  ;
            array[h] = array[h]+1  ;
            ini = (ini + 5)%7   ;
            }
            a= ((ini+5) % 7) ;
            pw.print(array[0] +  " " + array[1]+ " " + array[2]+ " "  + array[3]+ " " + array[4]+ " " + array[5]+ " " + array[6]+ " ")   ;
     
            pw.print(a) ;
            pw.close();
            s.close() ;
        }
    }
    and now Greedy gift giver program !!

    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.*;
     
    public class gift1 {
     
        public static void main(String[] args) throws IOException {
            Scanner s = new Scanner(new File("gift1.in"));
            PrintWriter pw = new PrintWriter(new File("gift1.out"));
            String  p , giver = null , taker ;
            int i , np , k , j ,h,total, no , calc,temp;
            np = s.nextInt() ;
            String names[] = new String[np] ;
            Hashtable <String , Object> table = new Hashtable<String, Object>();
            for (i = 2; i == (np + 1); i++) {
                p = s.next();
                names[i-2] = p ;
                table.put(p,0) ;
            }
     
            for (k = 1; k == np; k++) {
             giver = s.next() ;
             total = s.nextInt();
             no = s.nextInt() ;
             for (j=1 ; j == no ; j++){
                 if (no==0)
                  break ;
                 else
                 taker = s.next() ;
                 calc = ((total-(total%no))/no)  ;
                 temp = Integer.parseInt(table.get(taker).toString()) ;
                 temp = temp + calc ;
                 table.put(taker , temp) ;
                 temp = Integer.parseInt(table.get(taker).toString()) ;
                 temp = temp - calc ;
                 table.put(giver , temp) ;
                }
              }
            for(h=0 ; h<np ; h++ ) {
            pw.print(names[h]) ;
            giver = names[h] ;
            pw.print(table.get(giver)) ;
            }
            pw.close();
            s.close();
           }
     
    }
    Last edited by helloworld922; August 30th, 2010 at 06:47 PM.


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: problem !!

    Welcome to the forums! When you post code surround it in [highlight=java][/highlight] tags, it makes your code alot easier. It looks like the following line is your problem.
    Scanner s = new Scanner(new File("friday.in"));

    If you're trying to get the next int from a user then use:

    Scanner s = new Scanner(System.in);

    If your code is correct, then forgive me. It's 3am. I'll check your code again when I wake up.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem !!

    Try debugging your code by adding some more println()s. For example print out the values of a,b,c ... h inside of the loops.

    Also it helps when you have an error with the program's output to post the output here.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: problem !!

    I remember doing those. Fun times...

    Friday: I immediately see a bracket problem. Check that and we will work from there.
    Greedy: I also see a bracket problem. Check that and we can work from there aswell.

    @Brt93yoda: USACO uses input text files with the extensions .in and output text files with the extension .out. He is reading a file, not the console.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem !!

    first of all thanks to u both .....
    @Brt93yoda: as aussiemcgr said any way thanks
    @norm : i tried and the values out side the for loop are correct ...inside the loop are printed zeros and that what i dont know why ....i will try again
    @aussiemcgr : i dont think the brackets r problem as the two codes compiled successfully with me ....dont know !!

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: problem !!

    Ok, I think I was reading it wrong since its hard to read the code like that.

    Specifically where in the for loops do you print out the values? If we know exactly when they are 0, it is more helpful when debugging.

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem !!

    after incrementing the array elements

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem !!

    values out side the for loop are correct ...inside the loop are printed zeros
    Please post the print out from the program and post the new code with the println() statements.
    Be sure to put the code in code tags.

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: problem !!

    Ok, help me read through this.
    True or False (from Friday):
    for (i=0 ; i == n-1 ; i++ )
    This statement will only run once, since n is not reevaluated, and only if n equals 1 since i must equal 0 initially and n-1 must equal 0 for it to return true.

  10. #10
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: problem !!

    Quote Originally Posted by aussiemcgr View Post
    Ok, help me read through this.
    True or False (from Friday):

    This statement will only run once, since n is not reevaluated, and only if n equals 1 since i must equal 0 initially and n-1 must equal 0 for it to return true.
    He actually did that quiet a few times. I wonder if he means <=?

    for (i = 2; i == (np + 1); i++) { // Here
                p = s.next();
                names[i-2] = p ;
                table.put(p,0) ;
            }
     
            for (k = 1; k == np; k++) { // Here
             giver = s.next() ;
             total = s.nextInt();
             no = s.nextInt() ;
             for (j=1 ; j == no ; j++){ //Here
                 if (no==0)
                  break ;
                 else
                 taker = s.next() ;
                 calc = ((total-(total%no))/no)  ;
                 temp = Integer.parseInt(table.get(taker).toString()) ;
                 temp = temp + calc ;
                 table.put(taker , temp) ;
                 temp = Integer.parseInt(table.get(taker).toString()) ;
                 temp = temp - calc ;
                 table.put(giver , temp) ;
                }
              }

  11. The Following User Says Thank You to Brt93yoda For This Useful Post:

    planmaster (September 1st, 2010)

  12. #11
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem !!

    np >>> is the number of people that the problem give which is between1 to 10
    but no << urright ... it might be zero .... checkin again ..thanks

  13. #12
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem !!

    Quote Originally Posted by Brt93yoda View Post
    He actually did that quiet a few times. I wonder if he means <=?

    for (i = 2; i == (np + 1); i++) { // Here
                p = s.next();
                names[i-2] = p ;
                table.put(p,0) ;
            }
     
            for (k = 1; k == np; k++) { // Here
             giver = s.next() ;
             total = s.nextInt();
             no = s.nextInt() ;
             for (j=1 ; j == no ; j++){ //Here
                 if (no==0)
                  break ;
                 else
                 taker = s.next() ;
                 calc = ((total-(total%no))/no)  ;
                 temp = Integer.parseInt(table.get(taker).toString()) ;
                 temp = temp + calc ;
                 table.put(taker , temp) ;
                 temp = Integer.parseInt(table.get(taker).toString()) ;
                 temp = temp - calc ;
                 table.put(giver , temp) ;
                }
              }

    np >>> is the number of people that the problem give which is between1 to 10
    but no << urright ... it might be zero .... checkin again ..thanks