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 4 of 4

Thread: very hard algorithm implementation

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default very hard algorithm implementation

    So I wanted to solve this problem:

    After a tennis tournament each player was asked how many matches he had.
    An athlete can't play more than one match with another athlete.
    As an input the only thing you have is the number of athletes and the matches each athlete had. As an output you will have 1 if the tournament was possible to be done according to the athletes answers or 0 if not. For example:

    Input: 4 3 3 3 3 Output: 1
    Input: 6 2 4 5 5 2 1 Output: 0
    Input: 2 1 1 Output: 1
    Input: 1 0 Output: 0
    Input: 3 1 1 1 Output: 0
    Input: 3 2 2 0 Output: 0
    Input: 3 4 3 2 Output: 0

    the first number of the input is not part of the athletes answer it's the number of athletes that took part in the tournament for example in 6 2 4 5 5 2 1 we have 6 athletes that took part and their answers were 2 4 5 5 2 1

    This is what I have wrote so far and it is not working completely:

    import java.util.Scanner;
    import java.util.Arrays;
     
    public class Tennis {
     
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
     
            String N;
            int count;
            int sum = 0;
            int max;
            int activeAthletes;
            int flag;
     
            System.out.printf("Give: ");
            N = input.nextLine();
     
            String[] arr = N.split(" ");
            int[] array = new int[arr.length];
     
            for (count = 0; count < arr.length; count++) {
                array[count] = Integer.parseInt(arr[count]);
                //System.out.print(arr[count] + " ");
            }
     
            for (count = 1; count < arr.length; count++) {
                sum += array[count];
            }
            //System.out.println("\n" + sum);
     
            activeAthletes = array[0];
     
            for (count = 1; count < array.length; count++) {
                if (array[count] == 0) {
                    activeAthletes--;
                }
            }
     
            max = array[1];
            for (count = 2; count < array.length; count++) {
                if (array[count] > max) {
                    max = array[count];
                }
            }
           // System.out.println(max);
     
            if ((sum % 2 == 0) && (max < activeAthletes)) {            
                flag = 1;
            } else{
                flag = 0;
            }
     
            System.out.println(flag);
        }
    }


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: very hard algorithm implementation

    Hi TheByt3, I know I am replying a bit late, but I just saw your question, and think maybe I'd help you a bit. Here algorithm would work so:
    You should initially determine the maximum of the series you indicate, and decrease the other ones in the series by one, then we'd make the maximum number 0, and keep processing the same algorithm for the next maximum numbers remaine. For instance, say the series is as follows: 6 4 1 1 1 2 1. Here 6 indicates the number of players, we can start processing by the maximum number of the series, being 4. Decrease 1, 1, 1, and 2 by one so the result is 0 0 0 0 1 1. The maximum is, now, 1, afterwards the series get converted to 0 0 0 0 0 0, so the function should return the result true. The code is as follows (I hope it helps):

    javaYardim.txt

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: very hard algorithm implementation

    I'd better show the java code here to make it clearer. Regards.
    ...edited by moderator
    Last edited by copeg; July 26th, 2012 at 11:11 AM. Reason: spoonfeeding

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,308
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: very hard algorithm implementation

    Aivaz, please read the forum rules. And please read the following:
    The Problem with Spoon-feeding

Similar Threads

  1. I am having a hard time with my code
    By SandeeBee in forum What's Wrong With My Code?
    Replies: 14
    Last Post: November 12th, 2011, 08:34 AM
  2. Simple code apparently too hard for me...
    By Avopeb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 02:35 PM
  3. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM
  4. closest pair algorithm implementation
    By srose in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2011, 01:11 PM
  5. [SOLVED] Very complex project. It's hard to explain.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 55
    Last Post: November 4th, 2010, 11:30 AM

Tags for this Thread