在伪代码中表达两个变量之间的关系如何?

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

Expressing relationship between two variables in pseudocode?

问题

我有一些我正在尝试分析的伪代码

```python
public static void test(float z) {
  float y = 0;
  for (float i = 1; i <= z; i++) {
    if (y < z) {
      y = 4 * i * i + 6;
    }
  }
  return y;
}

从这个函数中,我理解当 y < z 时,y = 4i^2 + 6。然而,我在捕捉y和z之间的关系方程方面遇到了困难。我感觉它可以被表示为一个阶梯函数(分段函数)--对于z中的某个数字范围,y将具有指定的值。


<details>
<summary>英文:</summary>

I have some pseudocode I am trying to analyze:

public static void test(float z) {
float y = 0;
for (float i = 1; i <= z; i++) {
if (y < z) {
y = 4 * i * i + 6;
}
}
return y;
}

From the function, I understand that ```y = 4i^2 + 6``` whenever ```y &lt; z```. However, I am having trouble capturing the relationship between y and z in an equation. I feel that it could be captured as a floor function (step function) -- for a certain range of numbers in z, y will have that specified value. 

</details>


# 答案1
**得分**: 1

`y`首次变大于`z`(并停止变化)是在满足`2*i^2 + 3 > z`的第一个`i`。换句话说,最小的`i > sqrt((z - 3) / 2)`,这等于`floor(sqrt((z - 3)/2)) + 1`。现在,由于你已经知道了`i`的值,计算出`y`。

<details>
<summary>英文:</summary>

`y` becomes greater than `z` (and stops changing) for the first `i` such that `2*i^2 + 3 &gt; z`. In other words, a minimal `i &gt; sqrt((z - 3) / 2)`, which is `floor(sqrt((z - 3)/2)) + 1`. Now as you know `i`, compute `y`.

</details>



huangapple
  • 本文由 发表于 2020年8月28日 04:38:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63623849.html
匿名

发表评论

匿名网友

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

确定