美文网首页
Output Contest Matches

Output Contest Matches

作者: BLUE_fdf9 | 来源:发表于2018-09-27 02:21 被阅读0次

题目
During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the rank nth team, which is a good strategy to make the contest more interesting. Now, you're given n teams, you need to output their final contest matches in the form of a string.

The n teams are given in the form of positive integers from 1 to n, which represents their initial rank. (Rank 1 is the strongest team and Rank n is the weakest team.) We'll use parentheses('(', ')') and commas(',') to represent the contest team pairing - parentheses('(' , ')') for pairing and commas(',') for partition. During the pairing process in each round, you always need to follow the strategy of making the rather strong one pair with the rather weak one.

答案

class Solution {
    public String findContestMatch(int n) {
        String[] pair = new String[n];
        for(int i = 0; i < pair.length; i++)
            pair[i] = Integer.toString(i + 1);
        
        // First round there is 8 team, second round there is 
        for(; n > 1; n = n /2) {
            for(int i = 0; i < n / 2; i++) {
                int j = n - i - 1;
                // pair up i and j, put new string in i
                pair[i] = "(" + pair[i] + "," + pair[j] + ")";
            }            
        }
        return pair[0];
    }
}

相关文章

网友评论

      本文标题:Output Contest Matches

      本文链接:https://www.haomeiwen.com/subject/acesoftx.html