Q264: Count on Cantor
現代數學中有一個有名的證明(由Georg Cantor所提出的):有理數是可數的。他使用一個圖表(Cantor's 列舉)列舉出有理數,如下圖所示:
在此圖中,第一項是1/1,第2項是1/2,第三項是2/1,第四項是3/1,第五項是2/2,以下依此類推。
Input and Output
輸入每筆資料1行,含有1個正整數n (1<=n<=107)。
對每行輸入,輸出在Cantor's 列舉圖中的第n項。
對每行輸入,輸出在Cantor's 列舉圖中的第n項。
Sample Iutput
3
14
7
14
7
Sample Output
TERM 3 IS 2/1
TERM 14 IS 2/4
TERM 7 IS 1/4
TERM 14 IS 2/4
TERM 7 IS 1/4
import java.util.Scanner;
public class UVA_264 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(true){
int n=sc.nextInt();
int slash, term;
int part1, part2;
slash = 1;
term = 1;
while( term < n ) term += ++slash;
part1 = 1 + term - n;
part2 = slash - part1 + 1;
if(slash%2==1){
System.out.println("TERM "+n+" IS "+part1+"/"+part2);
}else{
System.out.println("TERM "+n+" IS "+part2+"/"+part1);
}
}
}
}
沒有留言:
張貼留言