任务总执行时长 华为OD真题 100

e0283e29e689f129ea4e556f7667c2d8.png
c2a4638e19824bfcf1e7ecf4ec7c6507.png

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);
    }
 
}

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];
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值