很久没写解题报告了,因为自己太懒了啊,懒得看英文题,懒得思考,懒得模拟,畏于攻坚,and so on...简而言之就是颓废了啊。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 982 | Accepted: 375 |
Description
A simple continuous fraction has the form:
where the ai’s are integer numbers.
The previous continuous fraction could be noted as [a1, a2, …, an]. It is not difficult to show that any rational number p⁄q, with integers p > q > 0, can be represented in a unique way by a simple continuous fraction with n terms, such that p⁄q = [a1, a2, …, an−1, 1], where n and the ai’s are positive natural numbers.
Your task is to find and print the simple continuous fraction that corresponds to a given rational number.
Input
Input will consist of a series of cases, each one in a line. A line describing a case contains p and q, two integer numbers separated by a space, with 1020 > p > q > 0.
The end of the input is indicated by a line containing 0 0
.
Output
Cases must be analyzed in the order that are read from the input. Output for each case will consist of several lines. The first line indicates the case number, starting at 1, using the format:
Case i:
replacing i
by the corresponding case number. The second line displays the input data in the form p / q
.
The remaining lines must contain the continuous fraction corresponding to the rational number, p⁄q, specified in the given input line. The continuous fraction must be printed accordingly to the following rules:
- Horizontal bars are formed by sequences of dashes ‘
-
’. - The width of each horizontal bar is exactly equal to the width of the denominator under it.
- Blank characters should be printed using periods ‘
.
’. - The number on a fraction numerator must be printed center justified. That is, the number of spaces at either side must be same, if possible; in other case, one more space must be added at the right side.
Sample Input
75 34 65 60 0 0
Sample Output
Case 1: 75 / 34 ..........1...... 2.+.------------- ............1.... ....4.+.--------- ..............1.. ........1.+.----- ................1 ............5.+.- ................1 Case 2: 65 / 60 ......1... 1.+.------ .........1 ....11.+.- .........1
Source
学长说这个题目很有意思,又是一个模拟题目,还是大数 :),那就很明确了,java大法好~( ̄▽ ̄~)~解题思路:把两个案例演算一遍,也就会感知到规律了,之后模拟即可。
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in);
int Case = 0;
BigInteger p, q;
while (true) {
p = cin.nextBigInteger();
q = cin.nextBigInteger();
if (p.equals(BigInteger.ZERO) && q.equals(BigInteger.ZERO))
break;
Case++;
System.out.println("Case " + Case + ":");
System.out.println(p + " / " + q);
int cnt = 0;
BigInteger quo, rem;
String[] s = new String[1005];
//处理部分:
for (;;) {
quo = p.divide(q);
rem = p.remainder(q);
p = q;
q = rem;
if (rem.equals(BigInteger.ZERO)) {
s[cnt] = quo.subtract(BigInteger.ONE).toString();
s[cnt] = s[cnt] + ".+.";
if (cnt == 0){
; //如果第一次就除尽了,就跳过else里那个for循环的处理,在这RE两次
}
else {
for (int i = 0; i < s[cnt - 1].length(); i++)
s[cnt] = "." + s[cnt];
}
cnt++;
break;
} else {
s[cnt] = quo.toString();
s[cnt] = s[cnt] + ".+.";
if (cnt >= 1) {
for (int i = 0; i < s[cnt - 1].length(); i++)
s[cnt] = "." + s[cnt];
}
cnt++;
}
}
//输出部分:
int len_max = s[cnt - 1].length() + 1;
for (int i = 0; i < cnt; i++) {
int len = s[i].length();
int tp = (len_max - len) / 2;
for (int j = 1; j <= len_max; j++) {
if (j == (len_max - tp))
System.out.print("1");
else {
System.out.print(".");
}
}
System.out.println();
System.out.print(s[i]);
for (int j = 0; j < len_max - len; j++) {
System.out.print("-");
}
System.out.println();
}
for (int i = 0; i < len_max - 1; i++)
System.out.print(".");
System.out.println("1");
}
}
}