Java 处理这个特定问题中的循环
我有一个问题: 机器人有三种类型 i、 )机器人A可以回收16个用过的瓶子-有20个机器人A ii.)机器人B可以回收32个用过的瓶子-有32个机器人B 机器人C可以回收128个用过的瓶子——有128个机器人C 我需要计算回收n个瓶子所需的最小机器人数量,并打印出这个数字 因此,我决定使用单独的方法调用以打印此号码:Java 处理这个特定问题中的循环,java,Java,我有一个问题: 机器人有三种类型 i、 )机器人A可以回收16个用过的瓶子-有20个机器人A ii.)机器人B可以回收32个用过的瓶子-有32个机器人B 机器人C可以回收128个用过的瓶子——有128个机器人C 我需要计算回收n个瓶子所需的最小机器人数量,并打印出这个数字 因此,我决定使用单独的方法调用以打印此号码: public class Q7 { public static void main(String[] args) { recycleBottles(16
public class Q7 {
public static void main(String[] args) {
recycleBottles(16);
}
public static void recycleBottles(int n) {
final int noOfAntrimRobots = 20;
final int noOfBelfastRobots = 10;
final int noOfCarrickfergusRobots = 3;
final int antrimRobotCapacity = 16;
final int belfastRobotCapacity = 32;
final int carrickfergusRobotCapacity = 128;
if (n % antrimRobotCapacity == 0 && n / antrimRobotCapacity <= noOfAntrimRobots) { // TO check if the bottles if completely divisible by Antrim robots and then print out the number of bottles
System.out.print("Number of Antrim Robots required: " + (n / antrimRobotCapacity));
} else if (n % belfastRobotCapacity == 0 && n / belfastRobotCapacity <= noOfBelfastRobots) {
System.out.print("Number of Belfast Robots required: " + (n / belfastRobotCapacity)); // TO check if the bottles if completely divisible by Belfast robots and then print out the number of bottles
} else if (n % carrickfergusRobotCapacity == 0 && n / carrickfergusRobotCapacity <= noOfCarrickfergusRobots) { // TO check if the bottles if completely divisible by Carrick Fergus robots and then print out the number of bottles
System.out.print("Number of Carrick Fergus Robots required: " + (n / carrickfergusRobotCapacity));
} else {
if (n > 128) {
int resultNum = 0;
while (n/128 < 4) {
resultNum = resultNum + (n/128);
}
}
}
}
}
公共类Q7{
公共静态void main(字符串[]args){
回收煤斗(16);
}
公共静态空隙回收箱(int n){
最终整数=20;
最终整数noOfBelfastRobots=10;
最终int noOfCarrickfergusRobots=3;
最终智能机器人容量=16;
最终国际贝尔法斯特机器人容量=32;
最终int carrickfergusRobotCapacity=128;
如果(n%antrimRobotCapacity==0&&n/antrimRobotCapacity尝试以下操作:
首先检查瓶子数量是否小于每个机器人的回收量
- 如果小于16,则使用一个机器人A进行操作
- 如果大于16且小于32,则使用机器人B进行操作
- 如果大于32且小于128,则使用机器人C执行此操作
如果大于128,则首先使用robot Cs执行,然后执行步骤1。再次执行
直到Cs用完。
然后使用机器人Bs执行此步骤,完成机器人Bs后,使用机器人As进行尝试
我认为这只是一个与最伟大的人分离的例子
int num = 2340;
int robC = num / 128;
num -= robC * 128;
int robB = num / 32;
num -= robB * 32;
int robA = num / 16;
num -= robA * 16;
if (num > 0) robA++;
System.out.println(robC);
System.out.println(robB);
System.out.println(robA);
我建议你首先用纸和铅笔自己解决这个问题。如果你不能用手来回答,你就没有办法编程。如果我问你回收3000个瓶子所需的机器人的最小数量,你会怎么做?你可能应该读一下如何问一个关于家庭作业的问题:一旦是answe,你就不要删除这个问题红色