VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > python教程 >
  • python基础教程之python中的迭代与递归

本站最新发布   Python从入门到精通|Python基础教程
试听地址  
https://www.xin3721.com/eschool/pythonxin3721/


遇到一个情况,需要进行递归操作,但是呢递归次数非常大,有一万多次。先不说一万多次递归,原来的测试代码是java的,没装jdk和编译环境,还是用python吧

先看下原本的java代码:

1
2
3
4
5
6
7
8
9
10
11
public class UpCount {
    private long calc(int depth) {
        if (depth == 0return 1;
        long cc = calc(depth - 1);
        return cc + (depth % 7) + ((((cc ^ depth) % 4) == 0) ? 1 0); 
    }
    public static void main(String[] args) {
        UpCount uc = new UpCount();
        System.out.println(uc.calc(11589));
    }
}

java没怎么玩过,但是这几行代码看过来还是没压力的,快刀斩乱麻改为对于python代码

1
2
3
4
5
6
7
8
9
10
11
12
def calc(depth):
    if depth == 0:
        return 1
    cc = long(calc(depth-1))
    xor_mod = (cc ^ depth)%4
    if xor_mod == 0:
        return cc+(depth%7)+1
    else:
        return cc+(depth%7)
 
number = long(calc(11589))
print number
相关教程