每日一题 0125

(2022.01.25) 每日一题:比赛中的配对次数

简单题!我重拳出击,啪的一下,非常的快,但是我写完看题解,又觉得自己是个傻子!

(1)自己的思路等同于官方题解一

模拟比赛的规则,啪的一下,写完了。

class Solution {
public:
    int numberOfMatches(int n) {
        int count = 0;
        while (n != 1){
            if(n % 2 ==0){
                count += n/2;
                n = n/2;
            }else{
                count += (n-1)/2;
                n = (n-1)/2 +1;
            }
        }
        return count;
    }
};

(2)脑筋急转弯,数学思路

在每一场比赛中,输的队伍无法晋级,且不会再参加后续的比赛。由于最后只决出一个获胜队伍,因此就有 n-1个无法晋级的队伍,也就是会有 n-1场比赛。

每淘汰一只队伍,就需要一场比赛,最后要淘汰n-1只队伍,就需要n-1次比赛。

class Solution {
public:
    int numberOfMatches(int n) {
        return n - 1;
    }
};
posted @ 2022-01-25 12:05  kusola  阅读(37)  评论(0)    收藏  举报