我有一个练习,我必须计算斐波那契数字直到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; }更多推荐
发布评论