我在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