Problem : Continuous Fractions
Description : 把一个假分数写成图中的形式。
Solution : 大数+模拟。小心就好。确定好图的尺寸和分数线的长度就好了。
Code(JAVA) :
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static final int M = 3000;
Scanner cin = new Scanner(System.in);
public Main() {
int K = 1;
while (cin.hasNext()) {
BigInteger a = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
BigInteger[] ans = new BigInteger[M];
int top = 0;
BigInteger up = a, down = b;
for (;;) {
ans[top] = up.divide(down);
if (up.mod(down).equals(BigInteger.ZERO)) {
ans[top] = ans[top].subtract(BigInteger.ONE);
break;
}
BigInteger tmp = down;
down = up.mod(tmp);
up = tmp;
++top;
}
++top;
char[][] map = new char[M][M];
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
map[i][j] = '.';
int R = top * 2 + 1, C = 1;
for (int i = top - 1; i >= 0; i--)
C += ans[i].toString().length() + 3;
int sum = 1;
int r = R - 1;
map[R][C] = '1';
for (int i = top - 1; i >= 0; i--) {
for (int j = 1; j <= sum; j++)
map[r][C - sum + j] = '-';
map[r - 1][C - sum + (sum + 1) / 2] = '1';
map[r][C - sum - 1] = '+';
String tmp = ans[i].toString();
int len = tmp.length();
for (int j = 0; j < len; j++)
map[r][C - (sum + 3 + len) + j + 1] = tmp.charAt(j);
sum += len + 3;
r -= 2;
}
System.out.println("Case " + (K++) + ":\n" + a + " / " + b);
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++)
System.out.print(map[i][j]);
System.out.println();
}
}
}
public static void main(String[] args) {
new Main();
}
}