📜  等级等于或小于给定截止等级的玩家数量(1)

📅  最后修改于: 2023-12-03 15:41:08.611000             🧑  作者: Mango

统计等级等于或小于给定截止等级的玩家数量

需求描述

一个游戏平台需要统计等级等于或小于给定截止等级的玩家数量。为了方便运营人员查看,需要将统计结果输出为markdown格式。

输入
  • n:整数,表示玩家数量。
  • players:长度为 n 的整数数组,表示每个玩家的等级。
  • cutoff:整数,表示截止等级。
输出
  • 返回markdown格式,包含以下内容:
    • 等于或小于截止等级的玩家数量为:xxx
实现
Python
def count_players(n: int, players: List[int], cutoff: int) -> str:
    count = sum(player <= cutoff for player in players)
    return f"等于或小于截止等级的玩家数量为:{count}"
Java
public static String countPlayers(int n, int[] players, int cutoff) {
    int count = 0;
    for (int player : players) {
        if (player <= cutoff) {
            count++;
        }
    }
    return "等于或小于截止等级的玩家数量为:" + count;
}
C++
#include <vector>
#include <string>
using namespace std;

string count_players(int n, vector<int>& players, int cutoff) {
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (players[i] <= cutoff) {
            count++;
        }
    }
    return "等于或小于截止等级的玩家数量为:" + to_string(count);
}
示例
Python
n = 5
players = [80, 70, 90, 60, 50]
cutoff = 75
print(count_players(n, players, cutoff))

输出结果为:

等于或小于截止等级的玩家数量为:3
Java
int n = 5;
int[] players = {80, 70, 90, 60, 50};
int cutoff = 75;
System.out.println(countPlayers(n, players, cutoff));

输出结果为:

等于或小于截止等级的玩家数量为:3
C++
int n = 5;
vector<int> players{80, 70, 90, 60, 50};
int cutoff = 75;
cout << count_players(n, players, cutoff) << endl;

输出结果为:

等于或小于截止等级的玩家数量为:3