C# 中的循环法

发布于 2024-08-02 04:24:34 字数 524 浏览 2 评论 0原文

我在尝试用 C# 解决数学问题时遇到了问题(可能是由于睡眠不足!)。

假设我有一台饮料机,并且有三排空的可以装可乐。我手里有 17 罐可乐,我必须一次填满每一排。

例如...

传递 1:

将可乐添加到第 1 行。饮料 = 1
将可乐添加到第 2 行。饮料 = 1
将可乐添加到第 3 行。饮料 = 1

通过 2:

将可乐添加到第 1 行。饮料 = 2
将可乐添加到第 2 行。饮料 = 2
将可乐添加到第 3 行。饮料 = 2

...

通过 6

将可乐添加到第 1 行。饮料 = 6
将可乐添加到第 2 行。饮料 = 6
将可乐添加到第 3 行。饮料 = 5(此时没有更多饮料了)

出于某种原因,我完全迷失了。有人可以帮忙吗?

I'm having a problem (possibly due to lack of sleep!) where I'm trying to solve a maths problem in C#.

Let's say I have a drinks machine, and I have three empty rows that can be filled with Cola. I have 17 cans of Cola in my hand and I have to fill each row one at a time.

For example...

Pass 1:

Add Cola to Row 1. Drinks = 1
Add Cola to Row 2. Drinks = 1
Add Cola to Row 3. Drinks = 1

Pass 2:

Add Cola to Row 1. Drinks = 2
Add Cola to Row 2. Drinks = 2
Add Cola to Row 3. Drinks = 2

...

Pass 6

Add Cola to Row 1. Drinks = 6
Add Cola to Row 2. Drinks = 6
Add Cola to Row 3. Drinks = 5 (no more drinks left at this point)

For some reason, I'm completely lost. Can anyone help?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仲春光 2024-08-09 04:24:34

非常快速且无痛,并且只需要一个循环,而不是两个嵌套循环。您所需要的只是一点数学知识来获取数组的正确索引:

int[] Cola = {0,0,0};
int Rows = Cola.Length;
int Drinks = 17;

for (int i = Drinks; i > 0; i--)
{
   Cola[(Drinks - i) % Rows]++;
}

Console.WriteLine("Row 1 has " + Cola[0] + " cans.");
Console.WriteLine("Row 2 has " + Cola[1] + " cans.");
Console.WriteLine("Row 3 has " + Cola[2] + " cans.");

这将产生以下输出:

Row 1 has 6 cans.
Row 2 has 6 cans.
Row 3 has 5 cans.

Pretty quick and painless, and only requires one loop, not two nested loops. All you need is a little bit of math to get the correct index of the array:

int[] Cola = {0,0,0};
int Rows = Cola.Length;
int Drinks = 17;

for (int i = Drinks; i > 0; i--)
{
   Cola[(Drinks - i) % Rows]++;
}

Console.WriteLine("Row 1 has " + Cola[0] + " cans.");
Console.WriteLine("Row 2 has " + Cola[1] + " cans.");
Console.WriteLine("Row 3 has " + Cola[2] + " cans.");

This produces this as output:

Row 1 has 6 cans.
Row 2 has 6 cans.
Row 3 has 5 cans.
青朷 2024-08-09 04:24:34

您可以计算每行将获得多少个罐头,而不是循环一次添加一个罐头:

int cans = 17;
cans += machine.Rows.Count;
for(int i = 1; i <= machine.Rows.Count; i++) {
   Console.WriteLine("Row {0} has {1} cans.", i, --cans / machine.Rows.Count);
}

Instead of looping to add a can at a time, you can calculate how many cans each row will get:

int cans = 17;
cans += machine.Rows.Count;
for(int i = 1; i <= machine.Rows.Count; i++) {
   Console.WriteLine("Row {0} has {1} cans.", i, --cans / machine.Rows.Count);
}
灯下孤影 2024-08-09 04:24:34

从臀部开始:

int numDrinks = /* Your constant here */
int[] drinksInRow = new int[NUM_ROWS];
for(int i = 0; i < drinksInRow.Length; i++)
{
  drinksInRow[i] = numDrinks / NUM_ROWS;
  if(i < numDrinks % NUM_ROWS) drinksInRow[i]++;
}

每行的饮料数量在 drinksInRow 中,按从 0 开始的行号索引。

这比重复传递要快;基本上是 O(NUM_ROWS) [如果使用 Big-O 玩真正松散的话]。

Shooting from the hip:

int numDrinks = /* Your constant here */
int[] drinksInRow = new int[NUM_ROWS];
for(int i = 0; i < drinksInRow.Length; i++)
{
  drinksInRow[i] = numDrinks / NUM_ROWS;
  if(i < numDrinks % NUM_ROWS) drinksInRow[i]++;
}

number of drinks in each row is in drinksInRow, indexed by row number starting from 0.

This is faster than doing repeated passes; basicallys its O(NUM_ROWS) [if playing really loose with Big-O].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文