这个循环执行了多少次 [英] how many times this loop executes
本文介绍了这个循环执行了多少次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
我想计算下面代码的顺序。但我的问题是我不知道循环执行的次数是多少次?
可以告诉我这个循环执行了多少次?
谢谢
while (n> 0)
{
r = n%m;
n = m;
m = r;
}
解决方案
没有人能告诉你:它取决于输入值。
如果n == 0,它将运行0次。
如果n == 1000且m == 10,它将在第二次传递时以零除零异常失败。
我们无法告诉你更糟糕的代码失败! :笑:
引用:可以告诉我这个循环执行了多少次? / blockquote>在幸运场景中,以下函数告诉您:
int how_many_times( int n, int m)
{
int count = 0 :
while (n> 0)
{
r = n%m;
n = m;
m = r;
++ count;
}
返回计数;
}
hi i want to calculate the order of below code . but my problem is that i don't know how many times the loop executes in worse case?
can any one tell me that how many times this loop execute?
thanks
while(n>0) { r=n%m; n=m; m=r; }
解决方案Nobody can tell you: it's dependent on the input values.
If n == 0, it will run 0 times.
if n == 1000 and m == 10, it will fail with a divide by zero exception on the second pass.
We can't tell you a worse case for code that fails! :laugh:
Quote:can any one tell me that how many times this loop execute?
On lucky scenarios, the following function tells you:
int how_many_times(int n, int m) { int count = 0: while(n>0) { r=n%m; n=m; m=r; ++count; } return count; }
这篇关于这个循环执行了多少次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文