0
点赞
收藏
分享

微信扫一扫

如何降低云计算成本?

描述

示例

算法思路1

整体思路是,先读取特性的优先级和测试用例覆盖的特性列表,然后计算每个测试用例的优先级,并将其与测试用例的索引存储到二维数组中。最后按照优先级和索引排序,输出测试用例的索引,即为执行顺序。 

  1. 首先从标准输入中读取了两个整数 nm,分别表示特性的数量和测试用例的数量。

  2. 创建了一个长度为 n 的数组 priorities,用于存储特性的优先级。

  3. 使用循环读取每个特性的优先级,并将其存储到 priorities 数组中。

  4. 通过 in.nextLine() 读取了一个空行,用于消耗掉换行符。

  5. 创建了一个二维数组 res,其行数为测试用例的数量 m,列数为2,用于存储测试用例的优先级和对应的索引。

  6. 使用循环遍历每个测试用例:

    • 使用 in.nextLine() 读取了一行输入,其中包含了一个测试用例覆盖的特性列表。
    • 使用 split(" ") 方法将输入拆分为特性的索引数组。
    • 使用流式处理将特性的索引转换为整数流,并对每个特性索引计算其对应特性的优先级,并将所有特性的优先级求和。
    • 将求和得到的优先级和当前测试用例的索引存储到 res 数组中。
  7. 使用 Arrays.sort()res 数组进行排序,排序规则为:

    • 首先按照测试用例的优先级从大到小排序。
    • 如果测试用例的优先级相同,则按照测试用例的索引从小到大排序。
  8. 使用循环遍历排序后的 res 数组,并输出测试用例的索引(ID),注意索引从0开始,需要加1。

答案1

import java.util.Arrays;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int m = in.nextInt();
        int[] priorities = new int[n];
        for (int i = 0; i < n; i++) {
            priorities[i] = in.nextInt();
        }
        in.nextLine();
        int[][] res = new int[m][2];
        for (int i = 0; i < m; i++) {
            int sum = Arrays.stream(in.nextLine().split(" ")).mapToInt(a -> {
                int idx = Integer.parseInt(a);
                return priorities[idx - 1];
            }).sum();
            res[i][0] = sum;
            res[i][1] = i;
        }
        Arrays.sort(res, (a,b) -> {
            return b[0]-a[0] == 0 ? a[1] - b[1] : b[0]-a[0];
        });
        for (int[] re : res) {
            System.out.println(re[1]+1);
        }
    }
}

详解1

int sum = Arrays.stream(in.nextLine().split(" ")).mapToInt(a -> { int idx = Integer.parseInt(a); return priorities[idx - 1]; }).sum(); 

Arrays.sort(res, (a,b) -> {
    return b[0]-a[0] == 0 ? a[1] - b[1] : b[0]-a[0];
}); 

Lambda表达式应用 

Lambda表达式详见Lambda表达式-CSDN博客

Stream流待更新……

算法思路2

  1. 读取输入并保存数据结构:

    • 读取特性的数量 N 和测试用例的数量 M。
    • 创建一个长度为 N 的数组,用于存储特性的优先级。
    • 创建一个长度为 M 的列表,其中每个元素是一个集合,表示每个测试用例覆盖的特性。
  2. 计算测试用例的优先级:

    • 遍历测试用例列表,对于每个测试用例,遍历其覆盖的特性集合,累加特性的优先级,得到测试用例的优先级。
  3. 按照规则排序测试用例:

    • 对测试用例列表进行排序,首先按照优先级从大到小排序,如果优先级相同,则按照测试用例的ID从小到大排序。
  4. 输出测试用例的执行顺序:

    • 按照排序后的顺序输出测试用例的ID。

答案2

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt(); // 特性的数量
        int M = scanner.nextInt(); // 测试用例的数量

        int[] priorities = new int[N + 1]; // 特性的优先级,索引从1开始
        for (int i = 1; i <= N; i++) {
            priorities[i] = scanner.nextInt();
        }
        scanner.nextLine(); // 消耗换行符
        List<TestCase> testCases = new ArrayList<>();
        for (int i = 1; i <= M; i++) {
            int priority = 0;
            String[] features = scanner.nextLine().split(" ");
            for (String feature : features) {
                int featureId = Integer.parseInt(feature);
                priority += priorities[featureId];
            }
            testCases.add(new TestCase(i, priority));
        }

        // 按照优先级从大到小排序,如果优先级相同则按照ID从小到大排序
        testCases.sort((a, b) -> a.priority != b.priority ? Integer.compare(b.priority, a.priority) : Integer.compare(a.id, b.id));

        // 输出测试用例的执行顺序
        for (TestCase testCase : testCases) {
            System.out.println(testCase.id);
        }

        scanner.close();
    }

    static class TestCase {
        int id; // 测试用例的ID
        int priority; // 测试用例的优先级

        public TestCase(int id, int priority) {
            this.id = id;
            this.priority = priority;
        }
    }
}

详解2

list列表接口的使用详见Java“集合框架”知识速成笔记学完就去实战(三)-CSDN博客文章浏览阅读1.1k次,点赞32次,收藏12次。吐血整理!本文为Java中集合类型的一些常见接口和实现类,包括List接口,Set接口和Map接口,重点介绍了HashSet实现类。https://blog.csdn.net/wwwwwmmn/article/details/136433958 

举报

相关推荐

云计算成本优化指南

0 条评论