处理这个特定问题中的循环。

huangapple go评论59阅读模式
英文:

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 &amp;&amp; n / antrimRobotCapacity &lt;= noOfAntrimRobots) {  // TO check if the bottles if completely divisible by Antrim robots and then print out the number of bottles
System.out.print(&quot;Number of Antrim Robots required: &quot; + (n / antrimRobotCapacity));
} else if (n % belfastRobotCapacity == 0 &amp;&amp; n / belfastRobotCapacity &lt;= noOfBelfastRobots) {
System.out.print(&quot;Number of Belfast Robots required: &quot; + (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 &amp;&amp; n / carrickfergusRobotCapacity &lt;= noOfCarrickfergusRobots) { // TO check if the bottles if completely divisible by Carrick Fergus robots and then print out the number of bottles
System.out.print(&quot;Number of Carrick Fergus Robots required: &quot; + (n / carrickfergusRobotCapacity));
} else {
if (n &gt; 128) {
int resultNum = 0;
while (n/128 &lt; 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 &gt; 0) robA++;
System.out.println(robC);
System.out.println(robB);
System.out.println(robA);

答案2

得分: -1

尝试一下:

  1. 首先检查瓶子的数量是否小于每个机器人的回收容量。

    • 如果小于 16,则使用机器人 A 完成。
    • 如果大于 16 且小于 32,则使用机器人 B 完成。
    • 如果大于 32 且小于 128,则使用机器人 C 完成。
  2. 如果大于 128,则首先使用机器人 C 完成,然后再执行步骤 1,直到机器人 C 用尽为止。
    然后使用机器人 B 进行此步骤,在机器人 B 完成后,再尝试使用机器人 A 完成。

英文:

Try this:

  1. 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
  2. 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.

huangapple
  • 本文由 发表于 2020年10月23日 08:34:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64492333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定