gpt4 book ai didi

c - 查找一系列数字中的当前步骤

转载 作者:行者123 更新时间:2023-11-30 21:10:04 24 4
gpt4 key购买 nike

我尝试在 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。

我的函数原型(prototype)给出了当前行的当前步骤:

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;
}

这只需要一个步骤,但不需要多个步骤......

最佳答案

编辑:-

这是一个一步解决方案。

您需要找到 currentLine 等于或小于以 3 开头的 AP 的前 r 项。总和超过 currentLine 的第一项就是 currentStep。

假设 currentLine 小于或等于 AP 前 r 项之和。

因此,currentLine <= 3 + ... + 3 + (r-1) * 1。

伪代码:-

initialStep = 3;  // initialStep is 3 in this case.
sum = currentLine; .
currentStep = 0;
while(sum > initialStep){
sum -= initialStep;
initialStep = intialStep + 1;
currentStep++;
}
requiredAnswer = currentStep + 1;

您的答案将是循环之后的 (currentStep + 1),因为当条件失败时循环将终止。

关于c - 查找一系列数字中的当前步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32422236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com