2023华为OD机试真题-MVP争夺战(JAVA、Python、C++)

题目描述:

在星球争霸篮球赛对抗赛中,强大的宇宙战队,希望每个人都能拿到MVP。

MVP的条件是,单场最高分得分获得者,可以并列,所以宇宙战队决定在比赛中,尽可能让更多的队员上场,且让所有有得分的队员得分都相同。

然而比赛过程中的每一分钟的得分都只能由某一个人包揽。

输入描述:

输入第一行为一个数字t,表示有得分的分钟数( 1 <= t <= 50),第二行为t个数字,代表每一分钟的得分p(1 <= p <= 50)

输出描述:

输出有得分的队员都是MVP时最少的MVP得分。

补充说明:

 收起

示例1

输入:

9
5 2 1 5 2 1 5 2 1
输出:

6
说明:

样例解释:一共4人得分,分别都为6分
5 + 1
5 + 1
5 + 1
2 + 2  + 2
 

#include <iostream>
using namespace std;
 
int main() {
    int m;//时间
    cin >> m;
 
    int sc[m];//得分数组
    int i = 0;
    while (cin >> sc[i++]) {
        if (cin.get() == '\n')
            break;
    }
 
    int sum = 0, max = 0;
    for (int j = 0; j < m; j++) {
        sum += sc[j];
        if (max < sc[j]) {
            max = sc[j];
        }
    }
 
    while (true) {
        if (sum % max == 0) {
            cout << max;
            break;
        } else {
            max++;
        }
    }
 
    return 0;
}
import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int t = Integer.parseInt(sc.nextLine());
            String[] s = sc.nextLine().split(" ");
            int[] scores = new int[t];
            for (int i = 0; i < s.length; i++) {
                scores[i] = Integer.parseInt(s[i]);
            }
            int sum = 0, max = 0;
            for (int i = 0; i < t; i++) {
                sum += scores[i];
                if (max < scores[i]) {
                    max = scores[i];
                }
            }
            while (true) {
                if (sum % max == 0) {
                    System.out.println(max);
                    break;
                } else {
                    max++;
                }
            }
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
题目要求编写一个能够查找充电设备组合的 Python 程序。下面我将给出一个满足题目要求的实现方案。 首先,我们需要明确题目的要求,即在一个给定的充电设备列表中,找到所有满足指定总功率的组合。我们可以使用递归的方式来解决这个问题。 首先,我们定义一个函数 find_combinations,接收三个参数:devices(充电设备列表)、target_power(目标总功率)和 current_combination(当前组合)。 该函数的基本思路如下: 1. 如果当前组合的总功率等于目标总功率,则输出当前组合。 2. 遍历充电设备列表,对每个设备,尝试将其加入当前组合。 3. 继续递归调用 find_combinations,继续寻找下一个设备的组合。 4. 在递归调用结束后,尝试将当前设备从当前组合中移除,继续寻找其他设备的组合。 下面是一个具体的实现方案: ```python def find_combinations(devices, target_power, current_combination): current_power = sum(current_combination) if current_power == target_power: print(current_combination) return for device in devices: if current_power + device <= target_power: current_combination.append(device) find_combinations(devices, target_power, current_combination) current_combination.remove(device) # backtracking devices = [2, 3, 4, 5, 6] target_power = 9 find_combinations(devices, target_power, []) ``` 在这个例子中,我们设定了一个设备列表 devices(为了简化,我们假设设备的功率都是正整数),以及一个目标总功率 target_power。程序会输出所有满足指定总功率的组合。 运行上面的代码,输出结果可能类似于: ``` [2, 2, 2, 3] [2, 2, 5] [2, 3, 4] [3, 6] ``` 这些组合分别是满足总功率为9的所有设备组合。 以上是一个能够查找充电设备组合的 Python 程序的实现,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值