如何把循环变成一个数学方程? [英] How to turn for loop into a math equation?
本文介绍了如何把循环变成一个数学方程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在程序中有以下两个循环,我想把它们写成数学方程,但是我有一些困难找到一个简单的方法:
I have the following two loops in my program and I am trying to write them out as math equations, but I'm having some difficulty finding a concise way to do so:
// Loop1
for (int i = 0; i < nr; i++) {
array[i] = nepr;
}
// Loop2
for (int i = 0; i < (nvr % nr); i++) {
array[i]++;
}
代码完全功能,但我试图表达这些循环我正在写的文档。
The code is completely function, but I'm trying to express these loops in a document I'm writing.
任何帮助将非常感激。
Any help would be very much appreciated.
到目前为止我有这个:
array[i] = nepr, i = 0,...,(nr-1)
但我不知道如何将第二个循环合并到方程式,第二个方程为它。
but I'm not sure how to incorporate the second loop into the equation, or write a second equation for it.
推荐答案
不完全清楚,这是你要求,但下面做同样的事情在一个
Not entirely clear that's what you are asking, but the following does the same thing in a single loop.
for (int i = 0, j = nvr % nr; i < nr; i++) {
array[i] = nepr + (i < j);
}
以上等同于...
In casual language without any loops the above is equivalent to...
让
j = nvr%nr
。假设阳性nr
和nvr
,0 <= j < nr
和:
-
array [i] = nepr + 1
i = 0 ... j-1
-
array [i] = nepr
i = j ... nr-1
array[i] = nepr + 1
fori = 0 ... j-1
array[i] = nepr
fori = j ... nr-1
这篇关于如何把循环变成一个数学方程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文