c while while循环在条件之前停止(c while loop stop before condition)

编程入门 行业动态 更新时间:2024-10-28 15:28:26
c while while循环在条件之前停止(c while loop stop before condition)

我有一个练习,我必须计算斐波那契数字直到100,然后打印它们。

我已经制作了这段代码:

#include <stdio.h> #include <stdlib.h> int main() { int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0; printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1); while (fibonacci <= 100){ fibonacci = fParcialone+fParcialtwo; printf("The %2dst Fibonacci number is %3d\n", i, fibonacci); fParcialtwo = fParcialone; fParcialone = fibonacci; i++; } return 0; }

如你所见,它打印出第12个数字,这个数字大于100.我理解为什么会这样做。

另一种方法是:

#include <stdio.h> #include <stdlib.h> int main() { int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0; printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1); while (fibonacci <= 100){ fibonacci = fParcialone+fParcialtwo; if (fibonacci > 100){ return 0; } printf("The %2dst Fibonacci number is %3d\n", i, fibonacci); fParcialtwo = fParcialone; fParcialone = fibonacci; i++; } return 0; }

现在它可以工作但是现在,在每个循环中,它进行两次比较,而不仅仅是一次,我相信这种方式使用了很多“处理器时间”(在这个例子中它很少但是在更大的范围内它可能会使区别)。

有一个更好的方法吗?

favolas

I have one exercise that I have to calculate fibonacci numbers until 100 and then print them.

I have made this code:

#include <stdio.h> #include <stdlib.h> int main() { int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0; printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1); while (fibonacci <= 100){ fibonacci = fParcialone+fParcialtwo; printf("The %2dst Fibonacci number is %3d\n", i, fibonacci); fParcialtwo = fParcialone; fParcialone = fibonacci; i++; } return 0; }

As you can see, it prints the 12th number and this number is greater than 100. I understand why it is doing that.

One alternative is to make this:

#include <stdio.h> #include <stdlib.h> int main() { int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0; printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1); while (fibonacci <= 100){ fibonacci = fParcialone+fParcialtwo; if (fibonacci > 100){ return 0; } printf("The %2dst Fibonacci number is %3d\n", i, fibonacci); fParcialtwo = fParcialone; fParcialone = fibonacci; i++; } return 0; }

And now it works but now, In every loop it as to make two comparisons and not just one and I believe that this way it uses to much "processor time" (in this example its to little but on a bigger scale it probably will make difference).

Is there a better way to do this?

favolas

最满意答案

我认为你可以使用do ... while(cond)。 它应该看起来像:

#include <stdio.h> #include <stdlib.h> int main() { int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0; printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1); fibonacci = fParcialone+fParcialtwo; do { printf("The %2dst Fibonacci number is %3d\n", i, fibonacci); fParcialtwo = fParcialone; fParcialone = fibonacci; i++; } while ((fibonacci = fParcialone+fParcialtwo) <= 100); return 0; }

I think that you can use a do ... while(cond). It should looks like :

#include <stdio.h> #include <stdlib.h> int main() { int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0; printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1); fibonacci = fParcialone+fParcialtwo; do { printf("The %2dst Fibonacci number is %3d\n", i, fibonacci); fParcialtwo = fParcialone; fParcialone = fibonacci; i++; } while ((fibonacci = fParcialone+fParcialtwo) <= 100); return 0; }

更多推荐

本文发布于:2023-07-29 14:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   loop   condition   stop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!