算法题:数与连分数

背景

...

...:“这个简单...我们还是去刚才的海边呗...”
...:"其实今晚...我是有一定要完成的事情的..." .,
威尼斯真的是一个美丽的城市...很小的时候我就听说这个地方..

这一天..从贝鲁特归来的商队..除了布匹和香辛料...还带来的东方的数字....
也有人曾经讨论过它们的历史...
只是很长时间这些都不被那些数学家们所重视..

人们怀着敬畏的心情..小心的审视着这些新奇的东西...
而它们..给生活在这片土地上的人们所带来的..是很大的帮助..
...

描述

写一个程序...可以实现在连分数和分数之间的互相转换...

样例1

样例输入1

[2;3,7]
51/22

样例输出1

51/22
[2;3,7]

限制

出题人不透露

提示

多组测试数据:
...每一个测试点有多组数据...数据的组数不超过100组...
对于Pascal里...可以这样子
while not eof do begin
...
end;
来实现这点..
(至于..C++里..我就不太清楚了...)
约分:
计算结果最后是要约分的...但是..在分数转向连分数的时候..
我们不保证输入的数据是约分.....
连分数的输入格式:
连分数的输入格式是按照wiki里写的...
第一个位置的分号应该是为了避免可能的歧义~..
English:
我们校内的时候又同学抱怨英文看不懂...可以访问这个页...蛮好用的...
或者在wiki左侧的语言栏里最下面找中文切换一下就行了...
Range:
数字的规模都很小...
也就是它们都不会超过longint范围里....
包括中间的数据...
连分数的项数也不会超过100项..
See also:

连分数的其它知识..可以阅读....至于这个.....


我的答案:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

class Main
{
    public static void main(String[] argv)
    {
        try {
            InputStreamReader inputStream = new InputStreamReader(System.in);
            BufferedReader buffer = new BufferedReader(inputStream);

            String line;
            while ((line = buffer.readLine()) != null) {
                if (line.indexOf('/') > 0) {
                    Fraction numberFraction = Fraction.loadFromString(line);
                    System.out.println(numberFraction.reduction().toContinuedFraction());
                } else if (line.indexOf('[') == 0) {
                    ContinuedFraction numberContinuedFraction = ContinuedFraction.loadFromString(line);
                    System.out.println(numberContinuedFraction.toFraction());
                }
            }
        } catch (Exception ex) {
            System.out.println("Error:" + ex.getMessage());
        }
    }
}

class ContinuedFraction
{
    long a;
    ArrayList<Long> b;
    ContinuedFraction(long a, ArrayList<Long> b)
    {
        this.a = a;
        this.b = b;
    }
    public static ContinuedFraction loadFromString(String str) throws Exception
    {
        //[2;3,7]
        //[3]
        String[] parts = str.substring(1, str.length() - 1).split(";");
        long a = Integer.parseInt(parts[0]);
        ArrayList<Long> b = new ArrayList<Long>();
        if (parts.length > 1) {
            String[] bParts = parts[1].split(",");
            for (String bStr:bParts) {
                b.add(new Long(bStr));
            }
        }
        return new ContinuedFraction(a, b);
    }
    public String toString()
    {
        StringBuilder strBuilder = new StringBuilder(";");
        for (long i:b) {
            strBuilder.append(i);
            strBuilder.append(",");
        }
        strBuilder.deleteCharAt(strBuilder.length() - 1);
        return "[" + a + strBuilder + "]";
    }
    public Fraction toFraction()
    {
        return calFraction(a);
    }
    //recursion
    private Fraction calFraction(long left)
    {
        try {
            long right = b.remove(0);
            return new Fraction(left, 1L).addFraction(calFraction(right).reciprocal());
        } catch (IndexOutOfBoundsException ex) {
            return new Fraction(left, 1L);
        }
    }
}

class Fraction
{
    long a;
    long b;
    Fraction(long a, long b)
    {
        this.a = a;
        this.b = b;
    }
    public Fraction reduction()
    {
        again:while (true) {
            long min = Math.min(a, b);
            for (long i = min; i > min / 2 && i > 1; i--) {
                if (a % i == 0 && b % i == 0) {
                    a /= i;
                    b /= i;
                    continue again;
                }
            }
            break;
        }
        return this;
    }
    public String toString()
    {
        return b == 1 ? a + "" : a + "/" + b;
    }
    public static Fraction loadFromString(String str) throws Exception
    {
        long a, b;
        String[] parts = str.split("/");
        a = new Long(parts[0]);
        b = new Long(parts[1]);
        return new Fraction(a, b);
    }
    public ContinuedFraction toContinuedFraction()
    {
        long number = a / b;
        ArrayList<Long> list = new ArrayList<Long>();
        long newA = a - b * number;
        long newB = b;
        while (true) {
            if (newA == 1) {
                list.add(newB);
                break;
            } else if (newA == 0) {
                break;
            }
            long division = newB / newA;
            list.add(division);
            newB = newB - division * newA;
            division = newA;
            newA = newB;
            newB = division;
        }
        return new ContinuedFraction(number, list);
    }
    public Fraction addFraction(Fraction add)
    {
        long oldB = b;
        a *= add.b;
        b *= add.b;
        add.a *= oldB;
        add.b *= oldB;
        a += add.a;
        return reduction();
    }
    public Fraction reciprocal()
    {
        long tmp = a;
        a = b;
        b = tmp;
        return this;
    }
}


猜你喜欢

转载自blog.csdn.net/loophome/article/details/79261390