poj 3197 Continuous Fractions(java大数)

这篇博客介绍了如何解决poj 3197问题,该问题涉及将给定的有理数表示为简单的连续分数。博主分享了解题思路,强调通过模拟计算找到规律,并提供了Java代码实现。
摘要由CSDN通过智能技术生成

很久没写解题报告了,因为自己太懒了啊,懒得看英文题,懒得思考,懒得模拟,畏于攻坚,and so on...简而言之就是颓废了啊。

Continuous Fractions
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 [a1a2, …, an]. It is not difficult to show that any rational number pq, with integers p > q > 0, can be represented in a unique way by a simple continuous fraction with n terms, such that pq = [a1a2, …, 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, pq, 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");

		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值