/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: lincans1
LANG: JAVA
TASK: frac1
*/
import java.io.*;
import java.util.*;
class Fraction implements Comparable<Fraction> {
private int molecular;
private int denominator;
public int getMolecular() {
return molecular;
}
public int getDenominator() {
return denominator;
}
public Fraction(int molecular, int denominator) {
this.molecular = molecular;
this.denominator = denominator;
}
public int compareTo (Fraction that) {
double ans = 1.0 * this.molecular / this.denominator - 1.0 * that.molecular / that.denominator;
return ans > 0 ? 1 : ans < 0 ? -1 : 0;
}
public String toString() {
return molecular + "/" + denominator;
}
}
public class frac1 {
public frac1() throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
BufferedReader f = new BufferedReader(new FileReader("frac1.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("frac1.out")));
// Use StringTokenizer vs. readLine/split -- lots faster
StringTokenizer st = new StringTokenizer(f.readLine());
int N = Integer.parseInt(st.nextToken());
Set<Fraction> fractions = new TreeSet<>();
Set<Double> set = new HashSet<>();
for (int i = 0; i <= N; i++) {
for (int j = i; j <= N; j++) {
if (j == 0) continue;
double frac = (double)i / (double)j;
if (!set.contains(frac)) {
set.add(frac);
fractions.add(new Fraction(i, j));
}
}
}
for (Fraction fraction : fractions) {
out.println(fraction);
}
out.close(); // close the output file
}
public static void main (String [] args) throws IOException {
new frac1();
}
}
[Java] Ordered Fractions
猜你喜欢
转载自blog.csdn.net/weixin_41714373/article/details/112634173
今日推荐
周排行