全网最全面的华为OD机试真题汇总,100%原题题库,不需要开会员即可查看全部内容,更多考题请查看真题库。
真题库:https://www.yuque.com/codernav.com/od
题目:任务总执行时长
知识点:数组、循环、递归
时间限制:1s 空间限制:256MB 限定语言:不限
题目描述:
任务编排服务负责对任务进行组合调度。参与编排的任务有两种类型,其中一种执行时长为taskA,另一种执行时长为taskB。任务一旦开始执行不能被打断,且任务可连续执行。服务每次可以编排num个任务。请编写一个方法,生成每次编排后的任务所有可能的总执行时长。
输入描述:
第1行输入分别为第1种任务执行时长taskA,第2种任务执行时长taskB,这次要编排的任务个数num,以逗号分隔。
输出描述:
数组形式返回所有总执行时时长,需要按从小到大排列。
补充说明:
每种任务的数量都大于本次可以编排的任务数量。
0 < taskA
0 < taskB
0 <= num <= 100000
示例1
输入:
1,2,3
输出:
[3, 4, 5, 6]
说明:
可以执行 3 次 taskA,得到结果 3;执行 2 次 taskA和 1 次 taskB,得到结果 4 。以此类推,得到最终结果。
解题思路:
算法一:
题目规定了只有两个任务taskA和taskB,所以可以直接用一层for循环就能搞定
如例1所示:1,2,3
有3(num+1)种方式:
A,A,A 时长 1+1+1 = 3;
A,A,B 时长 1+1+2 = 4;
A,B,B 时长 1+2+2 = 5;
B,B,B 时长 2+2+2 = 6;
结果为3,4,5,6
算法二:
也可以使用递归。
代码实现一:
package com.codernav.demo.hwod.exam;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
/**
* @title 任务总执行时长
* @Description 任务编排服务负责对任务进行组合调度。
* 参与编排的任务有两种类型,其中一种执行时长为taskA,另一种执行时长为taskB。
* 任务一旦开始执行不能被打断,且任务可连续执行。
* 服务每次可以编排num个任务。请编写一个方法,生成每次编排后的任务所有可能的总执行时长。
* @Author 开发者导航
* @website https://codernav.com
* @date 2023/5/13
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] strings = sc.nextLine().split(",");
int aTime = Integer.valueOf(strings[0]);
int bTime = Integer.valueOf(strings[1]);
int num = Integer.valueOf(strings[2]);
Set<Integer> total = new TreeSet<>();
int res;
for (int i = 0; i <= num; i++) {
res = aTime * (num - i) + i * bTime;
total.add(res);
}
System.out.println(total);
}
}
代码实现二:
package com.codernav.demo.hwod.exam;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
/**
* @title 任务总执行时长
* @Description 任务编排服务负责对任务进行组合调度。
* 参与编排的任务有两种类型,其中一种执行时长为taskA,另一种执行时长为taskB。
* 任务一旦开始执行不能被打断,且任务可连续执行。
* 服务每次可以编排num个任务。请编写一个方法,生成每次编排后的任务所有可能的总执行时长。
* @Author 开发者导航
* @website https://codernav.com
* @date 2023/5/13
*/
public class Main {
static int num;
static Set<Integer> resList = new TreeSet<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] strings = sc.nextLine().split(",");
int[] tasks = new int[2];
tasks[0] = Integer.valueOf(strings[0]); //taskA
tasks[1] = Integer.valueOf(strings[1]); //taskB
num = Integer.valueOf(strings[2]);
handle(tasks, 0, 0, 0);
System.out.println(resList);
}
/**
* @param tasks taskA和taskB的数组
* @param count 已执行任务的个数
* @param times 执行任务的时长
* @param index 执行任务的索引(0:taskA,1:taskB)
*/
public static void handle(int[] tasks, int count, int times, int index) {
if (count == num) {
resList.add(times);
} else {
for (int i = index; i < 2; i++) {
times += tasks[i];
index = resList.size() == 0 ? 0 : 1;
handle(tasks, count + 1, times, index);
times -= tasks[0];
}
}
}
}
代码实现三:
package com.codernav.demo.hwod.exam;
import java.util.*;
/**
* @title 任务总执行时长
* @Description 任务编排服务负责对任务进行组合调度。
* 参与编排的任务有两种类型,其中一种执行时长为taskA,另一种执行时长为taskB。
* 任务一旦开始执行不能被打断,且任务可连续执行。
* 服务每次可以编排num个任务。请编写一个方法,生成每次编排后的任务所有可能的总执行时长。
* @Author 开发者导航
* @website https://codernav.com
* @date 2023/5/13
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] input = in.nextLine().split(",");
int taskA = Integer.parseInt(input[0]);
int taskB = Integer.parseInt(input[1]);
int nums = Integer.parseInt(input[2]);
HashSet<Integer> set = new HashSet<>();
if (nums != 0) {
int countA = nums;
while (countA >= 0) {
int countB = nums - countA;
set.add(countA * taskA + countB * taskB);
countA--;
}
}
List<Integer> ans = new ArrayList<>(set);
Collections.sort(ans);
System.out.println(ans);
}
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...
