英文:
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 < 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 > z`. In other words, a minimal `i > sqrt((z - 3) / 2)`, which is `floor(sqrt((z - 3)/2)) + 1`. Now as you know `i`, compute `y`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论