字节跳动青训营——入营考核解答(持续更新中~~~)
考核内容:
在指定的题库中自主选择不少于 15 道算法题并完成解题,其中题目难度分配如下:
- 简单题不少于 10 道
- 中等题不少于 4 道
- 困难题不少于 1 道
解答代码
29. 蛋糕工厂(中等)
代码实现:
public class Main {
public static int solution(int m, int w, int p, int n) {
int days = 0;
int totalCakes = 0; // 已生产的蛋糕总数
while (totalCakes < n) {
int dailyProduction = m * w; // 当天的生产量
int newResources = dailyProduction; // 当天可以用来购买机器和工人的蛋糕数量
// 先计算能够购买的机器数量
int newMachines = newResources / p;
newResources %= p; // 更新剩余蛋糕数量
// 检查购买机器后是否还有足够蛋糕购买工人
if (newResources >= p) {
int newWorkers = newResources / p;
newResources %= p;
m += newMachines;
w += newWorkers;
} else {
// 如果不够购买工人,只增加机器数量
m += newMachines;
}
totalCakes += dailyProduction;
days++;
}
return days;
}
public static void main(String[] args) {
// 测试用例
System.out.println(solution(3, 1, 2, 12) == 3);
System.out.println(solution(10, 5, 30, 500)==7);
System.out.println(solution(3, 5, 30, 320)==22);
}
}
运行结果:
原文地址:https://blog.csdn.net/LiuYQi/article/details/143087814
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!