Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tennis tournament algorithm

Tags:

java

algorithm

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.

So far this is what we wrote but didn't work that great:

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);
    }
}

I do not want a straight solution just maybe some tips and hints because we really have no idea what else to do and I repeat even though I'll tag it as a homework (because I feel the moderators will close it again) it is not, it's just something my brother found and we are trying to solve.

Well many of you have answered and I'm really grateful but as I have work tomorrow I need to go to sleep, so I'll probably read the rest of the answers tomorrow and see what works

like image 960
Aki K Avatar asked Apr 25 '12 20:04

Aki K


People also ask

What is the UTR algorithm?

UTR is calculated by an algorithm using a player's last 30 eligible match scores from the last 12 months. For each eligible match, the algorithm calculates a match rating and a match weight; a player's UTR is the weighted average of all the match ratings.

What are the 4 formats of play in tennis?

There are four primary styles of singles play in tennis: aggressive baseliner, serve-and-volleyer, counterpuncher, and all-court player.

How good is a 5 UTR?

UNIVERSAL 5: Entry level male competitive tennis player….The adults at this level may be vulnerable in many areas, but will usually be able to do at least one thing fairly well. This may include a good slice backhand, decent serve, or a drop shot.

How accurate is UTR?

UTR Rating is a number that provides a real and accurate measurement of skill level. A player's UTR is a number between 1.00 and 16.50. One match result is all it takes to receive a projected UTR Rating. After approximately five matches, the rating becomes fully reliable.


1 Answers

Not sure if it works 100%, i would go like:

  1. Sort input
  2. for each element going from right to left in array (bigger to smaller)

    • based on value n of element at index i decrease n left elements by 1
    • return fail if cant decrease because you reached end of list or value 0
  3. return success.

This logic (if correct) can lead whit some modifications to O(N*log(N)) solution, but I currently think that that would be just too much for novice programmer.

EDIT:

This does not work correct on input
2 2 1 1

All steps are then (whitout sorting):

  1. while any element in list L not 0:

    • find largest element N in list L
    • decrease N other values in list L by 1 if value >= 1 (do not decrease this largest element)
      • return fail if failure at this step
    • set this element N on 0
  2. return OK

like image 91
Luka Rahne Avatar answered Sep 21 '22 09:09

Luka Rahne