英文:
Dealing with loops in this specific question
问题
我有这个问题:
有三种类型的机器人。
i.) 机器人A可以回收16个已用瓶子 - 有20台A型机器人。
ii.) 机器人B可以回收32个已用瓶子 - 有32台B型机器人。
iii.) 机器人C可以回收128个已用瓶子 - 有128台C型机器人。
我需要计算回收n个瓶子所需的最小机器人数量,并打印出这个数量。
因此,我决定使用一个单独的方法来调用以打印出这个数字:
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) { // 检查瓶子是否可以被 Antrim 机器人完全整除,然后打印瓶子数量
System.out.print("所需 Antrim 机器人数量: " + (n / antrimRobotCapacity));
} else if (n % belfastRobotCapacity == 0 && n / belfastRobotCapacity <= noOfBelfastRobots) {
System.out.print("所需 Belfast 机器人数量: " + (n / belfastRobotCapacity)); // 检查瓶子是否可以被 Belfast 机器人完全整除,然后打印瓶子数量
} else if (n % carrickfergusRobotCapacity == 0 && n / carrickfergusRobotCapacity <= noOfCarrickfergusRobots) { // 检查瓶子是否可以被 Carrick Fergus 机器人完全整除,然后打印瓶子数量
System.out.print("所需 Carrick Fergus 机器人数量: " + (n / carrickfergusRobotCapacity));
} else {
if (n > 128) {
int resultNum = 0;
while (n/128 < 4) {
resultNum = resultNum + (n/128);
}
}
}
}
}
在瓶子数量既不是16的倍数,也不是32的倍数,也不是128的倍数的情况下,我陷入了困境。到目前为止,我正在尝试判断瓶子是否超过128,然后检查128型机器人的数量是否不超过3,并减去瓶子数量,以便继续处理其他机器人。
我真的在这个阶段陷入了困境,如果有人能够在这个阶段给我一个初始提示,我会非常感谢。我已经花了几个小时来摸索,但毫无收获。我只需要一个继续的线索。
英文:
I have this question:
There are three types of robot.
i.) Robot A can recycle 16 used bottles - There are 20 Robot As.
ii.) Robot B can recycle 32 used bottles - There are 32 robots B.
iii.) Robot C can recycle 128 used bottles - There are 128 robots C.
I need to calculate the minimum number of robots that are needed to recycle n bottles and print out this number.
So I have decided to use a separate method to call to print out this number:
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);
}
}
}
}
}
I'm stuck on the part where the number is not a multiple of 16, 32 or 128. So far I'm trying to see if bottles are more than 128, then to check if no. of robot for 128 is not more than 3 and then deduct the amount of bottles, to just proceed with other robots.
I'm really stuck in this stage and I'd really help if someone can give me a kickstart at this stage. I've spent hours figuring out but it's been of no avail. I just need a clue to go further.
答案1
得分: 0
我认为这只是一个最大除法的情况
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);
英文:
I think this is just a case of dividing from greatest
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);
答案2
得分: -1
尝试一下:
-
首先检查瓶子的数量是否小于每个机器人的回收容量。
- 如果小于 16,则使用机器人 A 完成。
- 如果大于 16 且小于 32,则使用机器人 B 完成。
- 如果大于 32 且小于 128,则使用机器人 C 完成。
-
如果大于 128,则首先使用机器人 C 完成,然后再执行步骤 1,直到机器人 C 用尽为止。
然后使用机器人 B 进行此步骤,在机器人 B 完成后,再尝试使用机器人 A 完成。
英文:
Try this:
-
First check the count of bottles is less than the recycle volume of each robot.
- If it is less than 16 do it with one robot A
- If it is greater than 16 and less than 32 do it with robot B
- If it is greater than 32 and less than 128 do it with robot C
-
If it is greater than 128 then first do it with robot Cs then do Step 1. again
until the Cs run out.
Then do this step with robot Bs and after finishing robot Bs try it with robot As.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论