Code conversion help: 将数字尽可能均分为相等的部分

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

Code conversion help Split number into as equal as possible parts

问题

Sure, here is the translated text you requested:

"有人可以解释一下,我如何将这段 Ruby 代码转换成 VB.NET 吗?因为我认为这正是我所需要的。我试图将一个数字分成尽可能相等的部分。

如果所有部分不能相等,因为数字不能相互整除,比如 32 除以 3。这个结果应该给我 11、11 和 10,这是尽可能相等的。

我在这里找到了一些代码,可以实现这个功能 https://stackoverflow.com/questions/36414923/split-number-into-equal-parts-or-whats-closest-to-that

Function SplitInto(n As Integer, p As Integer) As List(Of Integer)
    Dim result As New List(Of Integer)
    Dim quotient = n \ p
    Dim remainder = n Mod p
    
    For i As Integer = 1 To remainder
        result.Add(quotient + 1)
    Next
    
    For i As Integer = remainder + 1 To p
        result.Add(quotient)
    Next
    
    Return result
End Function

Dim parts As List(Of Integer) = SplitInto(32, 3)
Console.WriteLine(String.Join(", ", parts)) ' => 11, 11, 10

所以现在我面临的问题是,我不理解 Ruby 中发生了什么,因此无法将其转换。

在此先感谢任何帮助。"

英文:

Hi can anybody explain to me how i can convert this code in ruby to vb.net cause i think this is what i need. I am trying to divide a number into as equal as possible parts.

If all parts cant be equal because the numbers cant be divided by eachother ex. 32 div by 3. The result of this should give me 11, 11 and 10 this is as equal as possible.

I found some code here that does this https://stackoverflow.com/questions/36414923/split-number-into-equal-parts-or-whats-closest-to-that

def split_into n, p
    [n/p + 1] * (n%p) + [n/p] * (p - n%p)
end

print (split_into 32, 3) # => [11, 11, 10]

So now i have the issue that i do not understand whats happening here in ruby and so am unable to convert it.

Thanks for any help in advance.

答案1

得分: 5

Here is the translated content:

> 我不理解Ruby中发生的情况,因此无法转换它

我会尝试解释发生了什么,以便您可以进行转换。

当使用欧几里德除法将32除以3时,您有:

32 = 3 × 10 + 2

其中10是_商_,2是_余数_。

更一般地说:

a = d × q + r

要获得3个“相等”的部分,您必须分配这2个:

32 = 11 + 11 + 10
   = 2 × 11 + 1 × 10
   = 2 × (10+1) + (3-2) × 10

或更一般地:

a = r × (q+1) + (d-r) × q

在Ruby中,这些部分可以表示为:

[11] * 2 + [10] * 1
#=> [11, 11, 10]

在上面的代码中,[11]是一个数组,*复制其内容,即[11] * 2[11, 11]。对于[10] * 1,也是一样的,它只是[10]。最后,通过+连接两个数组,即[11, 11] + [10],返回[11, 11, 10]

或更一般地:

a = 32
d = 3
q = a / d #=> 10
r = a % d #=> 2

[q+1] * r + [q] * (d-r)
#=> [11, 11, 10]

将上述内容与您的[n/p + 1] * (n%p) + [n/p] * (p - n%p)进行比较,您将看到它是等效的(napdn/pqn%pr)。

如果不使用数组运算符,您可以使用两个循环来添加11和10:

parts = []

r.times { parts << q+1 }
(d-r).times { parts << q }

parts #=> [11, 11, 10]

希望这能帮助您理解这段代码。

英文:

> i do not understand whats happening here in ruby and so am unable to convert it

I'll try to explain what's going on so you can convert it.

When dividing 32 by 3 using Euclidean division you have:

32 = 3 × 10 + 2

With 10 being the quotient and 2 being the remainder.

Or more general:

a = d × q + r

To get 3 "equal" parts, you have to distribute those 2:

32 = 11 + 11 + 10
   = 2 × 11 + 1 × 10
   = 2 × (10+1) + (3-2) × 10

Or more general:

a = r × (q+1) + (d-r) × q

In Ruby, these parts can be expressed as:

[11] * 2 + [10] * 1
#=> [11, 11, 10]

In the above, [11] is an array and * duplicates its content, i.e. [11] * 2 is [11, 11]. The same happens for [10] * 1 which is just [10]. Finally, both arrays are concatenated via +, i.e. [11, 11] + [10] which returns [11, 11, 10].

Or more general:

a = 32
d = 3
q = a / d #=> 10
r = a % d #=> 2

[q+1] * r + [q] * (d-r)
#=> [11, 11, 10]

Compare the above with your [n/p + 1] * (n%p) + [n/p] * (p - n%p) and you'll see that it's equivalent (n is a, p is d, n/p is q and n%p is r).

Without array operators, you could use two loops to add the 11's and 10's:

parts = []

r.times { parts << q+1 }
(d-r).times { parts << q }

parts #=> [11, 11, 10]

huangapple
  • 本文由 发表于 2023年5月10日 16:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216611.html
匿名

发表评论

匿名网友

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

确定