如何将递归方法重新编码为迭代方法. [英] How to recode a recursive method to an iterative method.

查看:53
本文介绍了如何将递归方法重新编码为迭代方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>public static double recur(int n)
{  
   if(n==0)
     return 1;
   else
     if(n>0)
       return (recur(n-1)*10);
     else
       return (1/recur(-n);
}

推荐答案

类似的方法可能会解决问题;

Something like this might do the trick;

public static double iter(int n)
{
  double r = 1;
  if(n > 0){
    for(int i = 0; i < n; ++i)
      r *= 10;
  }
  else {
    for(int i = 0; i < n; ++i)
      r /= 10;
  }
  return r;
}



希望这会有所帮助,
弗雷德里克(Fredrik)



Hope this helps,
Fredrik


这篇关于如何将递归方法重新编码为迭代方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆