Java算法编程题

**

求助,Java编程。

**
有n个客户需要分发礼物,现有甲、乙两个仓库,两个仓库的库存分别为a、b,(0<a,b<=n<=a+b),d[i]为甲仓库给第i个客户发礼物的快递费,p[i]为乙仓库给第i个客户发礼物的快递费,问总收费最小。

输入:
第一行为n个客户
第二行为甲乙仓库的库存
接着是甲乙仓库给n个客户发货的收费标准
3
1 2
13 19
4 9
10 20

输出:
38
最小
19+9+10=38

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        Scanner scab = new Scanner(System.in);
        String str = scab.nextLine();
        int a = Integer.parseInt(str.split(" ")[0]);
        int b = Integer.parseInt(str.split(" ")[1]);
        int[][] cost = new int[n][2];
        for (int i = 0; i < cost.length; i++) {
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
            String[] s1 = s.split(" ");
            for (int j = 0; j < cost[i].length; j++) {
                cost[i][j] = Integer.parseInt(s1[j]);
            }
        }

//        int n = 3, a = 2, b = 2;
//        int[][] cost = {
//                {13, 19},
//                {9, 4},
//                {20, 10}
//        };

        for (int j = 0; j < n; j++) {
            for (int i = 1; i < n; i++) {
                    int[] temp = new int[2];
                    if ((cost[i][0] - cost[i][1]) < (cost[i - 1][0] - cost[i - 1][1])) {
                        temp[0] = cost[i - 1][0];
                        temp[1] = cost[i - 1][1];
                        cost[i - 1][0] = cost[i][0];
                        cost[i - 1][1] = cost[i][1];
                        cost[i][0] = temp[0];
                        cost[i][1] = temp[1];
                }
            }
        }
        for (int i = 0; i < cost.length; i++) {
            for (int j = 0; j < cost[i].length; j++) {
                System.out.print(cost[i][j] + " ");
            }
            System.out.println();
        }

        int sum = 0;
        for (int i = 0; i < n; i++) {
            if (i < a) {
                sum += cost[i][0];
                System.out.println(sum);
            } else {
                sum += cost[i][1];
            }
        }
        System.out.println(sum);
        
    }

}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值