从Java 转换到Python 的坎托函数

0 投票
1 回答
674 浏览
提问于 2025-04-17 22:48

我在Java中写了一个Cantor函数,现在想把它翻译成Python。有没有人能给我一些建议?我应该把分数作为一个整体来比较,还是把分子和分母当作两个独立的变量来处理呢?

Java代码:

    import java.util.Scanner;
    public class asdf {
     public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("ENter the nth ratio you want: ");
    int end = keyboard.nextInt();
    int count = 0;
    int top = 0;
    int bottom = 0;
    boolean flip = true;
    for(int i=0; i<end; i++){
        if(flip){
            top = i+1;
            bottom = 1;
            for(int j = 0; j<i+1; j++){
                if(count == end){
                    break;
                }
                System.out.println(top + "/" + bottom);
                top--;
                bottom++;
                count++;
            }
            flip = false;
        }
        else{
            top = 1;
            bottom = i+1;
            for(int j = 0; j<i+1; j++){
                if(count == end){
                    break;
                }
                System.out.println(top + "/" + bottom);
                top++;
                bottom--;
                count++;
                flip = true;
            }
        }
    }
}
    }

Python代码:

    def cantor(dat):
    n=len(dat)
    count, top, bottom = 0,0,0
    boolean flip = true
    for i in range (0,x):
            if (flip):
                    top = i+1
                    bottom = 1
                    for j in range(0,top):
                            if count == 21: break
                            boolean found = false
                            double temp = top*1.0/bottom
                            for k in range(0,n)):
                                    if temp == n[k]:
                                            found = true
                                            break
                    if !
         top = top-1
         bottom = bottom+1
         count = count+1

1 个回答

0

AgileUML的java2py3选项应该可以完成这个转换。它能生成格式正确的Python3代码。

https://github.com/eclipse/agileuml/blob/master/translators.zip

撰写回答