我嘗試在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