aiaiask.com©

开花即结果 提问即答案

高效时间管理:提升工作生活品质秘诀
原始问题:

高效时间管理:提升工作生活品质秘诀

以下是使用Java语言实现该功能的代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt(); // 询问次数

        for (int i = 0; i < t; i++) {
            int n = scanner.nextInt(); // 数组元素个数
            int[] arr = new int[n]; // 数组

            for (int j = 0; j < n; j++) {
                arr[j] = scanner.nextInt();
            }

            boolean canMakeProductEven = canMakeProductEven(arr);
            System.out.println(canMakeProductEven ? "YES" : "NO");
        }
    }

    // 判断是否可以通过一次操作使得所有元素的乘积为偶数
    private static boolean canMakeProductEven(int[] arr) {
        int oddCount = 0; // 奇数个数
        int evenCount = 0; // 偶数个数

        for (int num : arr) {
            if (num % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }

        // 如果数组中有偶数,则一次操作即可使得所有元素的乘积为偶数
        if (evenCount > 0) {
            return true;
        }

        // 如果数组中只有奇数个数大于等于2,则一次操作即可使得所有元素的乘积为偶数
        if (oddCount >= 2) {
            return true;
        }

        return false;
    }
}

输入示例:

2
4
1 2 3 4
3
1 3 5

输出示例:

YES
NO

解释:

  • 第一组询问中,数组为 [1, 2, 3, 4],可以选择将 1 和 3 变成它们的平均数 2,得到数组 [2, 2, 2, 4],所有元素的乘积为偶数,所以输出 YES。
  • 第二组询问中,数组为 [1, 3, 5],无法通过一次操作使得所有元素的乘积为偶数,所以输出 NO。
高效时间管理:提升工作生活品质秘诀
https://www.aiaiask.com/ask/44852.html
AI / 模型DeepSeek
时间 / DATEAugust 19, 2023, 7:53 PM
语言zh-CN
IP / 区域安徽 合肥