gpt4 book ai didi

c - 在c中递减while循环

转载 作者:太空宇宙 更新时间:2023-11-04 06:41:29 25 4
gpt4 key购买 nike

是否有可能在 C 的 while 循环中将数组大小减少超过 x--。例如,您能否在每次迭代中将数组递减三分之一的数组大小?

int n = 10;

while (n < 0)

// do something

(round(n/3))-- // this doesn't work, but can this idea be expressed in C?

谢谢你的帮助!

最佳答案

您可以使用任何表达式:

int n = 10;
while (n > 0) // Note change compared with original!
{
// Do something
n = round(n/3.0) - 1; // Note assignment and floating point
}

请注意,您只能递减变量,不能递减表达式。

您还可以使用 for 循环:

for (int n = 10; n > 0; n = round(n/3.0) - 1)
{
// Do something
}

在这种情况下,无论是否使用 float 进行舍入,n 的值序列都是相同的 (n = 10, 2),因此您可以写:

n = n / 3 - 1;

您会看到相同的结果。对于其他上限,顺序会发生变化 (n = 11, 3)。这两种技术都很好,但您需要确定自己知道自己想要什么,仅此而已。

关于c - 在c中递减while循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7604877/

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