牛客网 2018校招真题 搜狐 包裹运输

Description

牛客网 2018校招真题 包裹运输

Solving Ideas
  • 对于 6 × 6 的产品,每一个都需要一个包裹,而且无法填充
  • 对于 5 × 5 的产品, 每一个都需要一个包裹,且包裹余下空间只能填充 1 × 1 的产品
  • 对于 4 × 4 的产品, 每一个都需要一个包裹,余下空间能填充 2 × 2 或 1 × 1 的产品,根据贪心的思想,优先并尽可能地填充 size 更大的产品
  • 对于3 × 3 的产品,一个包裹最多能填充 4 个 3 × 3 的产品,如果 3 × 3 的产品不能填充满一个包裹,则余下空间能填充 2 × 2 或 1 × 1 的产品,同样优先填充 size 更大的产品
  • 对于 2 × 2 的产品,一个包裹最多能填充 9 个 2 × 2 的产品,如果 2 × 2 的产品不能填充满一个包裹,则余下空间能填充 1 × 1 的产品
  • 最后计算需要多少个包裹来填充 1 × 1 的产品
Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author wylu
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = br.readLine()) != null) {
            String[] strs = line.split(" ");
            int[] counts = new int[strs.length];
            boolean flag = true;
            for (int i = 0; i < strs.length; i++) {
                counts[i] = Integer.parseInt(strs[i]);
                if (flag && counts[i] != 0) flag = false;
            }
            if (flag) return;

            // 6 * 6
            int res = counts[5];

            // 5 * 5
            res += counts[4];
            counts[0] -= counts[4] * 11; //余下空间用来填充 1 × 1 的产品

            // 4 * 4
            res += counts[3];
            int fillTwo = counts[3] * 5;
            // 先尽可能地用 2 × 2 的进行填充
            counts[1] -= (fillTwo >= counts[1]) ? counts[1] : fillTwo;
            // 然后用 1 × 1 的进行填充
            counts[0] -= (fillTwo >= counts[1]) ? (fillTwo - counts[1]) : 0;

            // 3 * 3
            int rmd = counts[2] % 4;
            res += (counts[2] / 4 + (rmd == 0 ? 0 : 1));
            int[] two = {0, 5, 3, 1}, one = {0, 27, 18, 9};
            if (rmd > 0) {
            	// 先尽可能地用 2 × 2 的进行填充
                counts[1] -= (two[rmd] >= counts[1]) ? counts[1] : two[rmd];
                // 然后用 1 × 1 的进行填充
                counts[0] -= (two[rmd] >= counts[1]) ? (one[rmd] - counts[1] * 4) : (8 - rmd);
            }

            // 2 * 2
            res += (counts[1] / 9  + (counts[1] % 9 == 0 ? 0 : 1));
            if (counts[1] % 9 > 0) counts[0] -= (36 - counts[1] % 9 * 4);

            // 1 * 1
            if (counts[1] > 0) res += (counts[0] / 36 + (counts[0] % 36 == 0 ? 0 : 1));
            System.out.println(res);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值