USACO Ordered Fractions
表示这种算法,我想不到、目前还没完全理解
1 /* 2 ID: rrqqgg2 3 LANG: JAVA 4 TASK: frac1 5 */ 6 import java.io.BufferedReader; 7 import java.io.BufferedWriter; 8 import java.io.FileReader; 9 import java.io.FileWriter; 10 import java.io.IOException; 11 import java.io.PrintWriter; 12 import java.util.StringTokenizer; 13 14 public class frac1 { 15 static int n; 16 17 static void print(int a1, int a2, int b1, int b2) { 18 if (b1 + b2 > n) 19 return; 20 print(a1, a1 + a2, b1, b1 + b2); 21 out.println((a1 + a2) + "/" + (b1 + b2)); 22 print(a1 + a2, a2, b1 + b2, b2); 23 } 24 25 static PrintWriter out; 26 27 public static void main(String[] args) throws IOException { 28 // Use BufferedReader rather than RandomAccessFile; it's much faster 29 BufferedReader f = new BufferedReader(new FileReader("frac1.in")); 30 // input file name goes above 31 out = new PrintWriter(new BufferedWriter(new FileWriter("frac1.out"))); 32 // Use StringTokenizer vs. readLine/split -- lots faster 33 n = Integer.parseInt((new StringTokenizer(f.readLine())).nextToken()); 34 out.println("0/1"); 35 print(0, 1, 1, 1); 36 out.println("1/1"); 37 out.close(); // close the output file 38 System.exit(0); // don't omit this! 39 40 } 41 }