我尝试在C中创建一个简单的函数,它允许我们通过给出当前行号,行总数以及行数来找到一系列数字的当前步骤。步骤...的数字系列查找当前步骤中的一系列数字
例子:
line 0 - 0 --
line 1 - 0 |--> STEP 1
line 2 - 0 --
line 3 - 1 --
line 4 - 1 |--> STEP 2
line 5 - 1 |
line 6 - 1 --
line 7 - 2 --
line 8 - 2 |
line 9 - 2 |--> STEP 3
line 10 - 2 |
line 11 - 2 --
Parameters : currentLine = 5; totalLines = 12; steps = 3;
我在这种情况下,三个不同的步骤,所有步骤由一个多线递增。每一步都用相同的数字表示,紧靠行号。
在我的示例中,我选择currentLine = 5,它代表我们想要查找当前步骤的行。所以,在我的情况,我需要找到:2
我的我的功能,给予当前行的当前步骤的原型:
int findCurrentStep(int currentLine, int totalLines, int steps);
我只是想知道如何计算呢?
编辑:谢谢你的回答,我只是提出了另一种方法。
int findCurrentStep(int currentLine, int totalLines, int steps)
{
int step;
int trim_lines;
step = steps;
trim_lines = totalLines;
while (currentLine <= trim_lines -1)
{
trim_lines = totalLines - 3 + steps - 1;
step--;
}
return step;
}
这适用于只需一个步骤,但没有几个的...
那么,你到目前为止尝试过什么?为什么目前的解决方案没有工作,如果有的话? – Tlacenka
你是什么意思?“在这种情况下,我有三个不同的步骤,所有步骤都增加一行。” – vish4071
@ vish4071我认为通过递增步数,行数加1(步0 - 3行,步1 - 4行...) – Tlacenka