在虚拟温度单位和输出分数之间进行转换

时间:2020-05-20 12:20:33

标签: java double fractions units-of-measurement temperature

我目前正在研究一些Java代码,这些代码可以在温度单位之间转换温度,并以降低的分数输出新温度。

它的工作方式是首先采用等于0摄氏度和100摄氏度(冻结和沸腾)的单位系统。

32 212 //Unit 1
88 7   //Unit 2

重要的一点是,所有这些单位系统都以线性方式与摄氏温度单位相关。

然后接受输入a,b和c,这意味着将单位a中的c度转换为单位b中的度。

1 2 212

应将上述第一单元的212度转换为第二单元的温度,在这种情况下为7。

然后我必须将这个新温度转换为分数。在我的示例中,新温度为7,因此我返回7/1

我首先要做的是将unit1的温度转换为摄氏温度。
然后将摄氏温度转换为unit2。
最后将这个双精度转换为分数。

但是,我的问题是它在某些测试用例中失败了,我怀疑将double转换为分数时会失败。有人知道我的方法是否有缺陷吗?

任何帮助将不胜感激。我不是在找人来修复我的代码,而是要向我指出正确的方向。我本人无法解决。

这是我到目前为止要做的:

public class Temperature {


    public static double[][] temps;

    public static void main(String[] args) {
        Kattio io = new Kattio(System.in, System.out);

        int N = io.getInt();
        int Q = io.getInt();

        // temps[unit][0] is freezing
        // temps[unit][1] is boiling
        // temps[unit][2] is for converting from
        // temps[unit][3] is for converting to
        temps = new double[N+1][4];


        for(int i = 0; i < N; i++) {
            double t1 = io.getInt();
            double t2 = io.getInt();
            temps[i+1][0] = t1;
            temps[i+1][1] = t2;

            temps[i+1][2] = (t2-t1)/100;
            temps[i+1][3] = 100/(t2-t1);
        }

        for(int i = 0; i < Q; i++) {
            // from is unit we are converting from
            int from = io.getInt();

            // to is unit converting to
            int to = io.getInt();

            // t is temperature in unit "from"
            double t = io.getInt();

            // get converted as double -> later converted to fraction
            double converted = solve(from, to, t);

            
            String ans = getFraction(converted);
            
            
            System.out.println(ans);
            
        }
    }
    
    // Get the new temperature as a double
    public static double solve(int from, int to, double t) {


        // Convert to celsius first
        double a = temps[from][3];

        // Take temperatur and subtract freezing point temp before 
        // multiplying with rate
        double celsius = (t - temps[from][0]) * a;


        // Convert the celsius to desired unit
        double b = temps[to][2];

        // Multiply temp with rate before adding freezing temp value
        double degrees = (b * celsius) + temps[to][0];

        // Return double of degrees in new unit
        return degrees;   
        
    }

    //Turn double into fraction
    public static String getFraction(double x){

        // If we have a negative number
        if (x < 0){
            return "-" + getFraction(-x);
        }

        double tolerance = 1.0E-6;

        double h1=1; 
        double h2=0;
        double k1=0; 
        double k2=1;

        double b = x;
        do {
            double a = Math.floor(b);
            double aux = h1; 
            h1 = a*h1+h2; 
            h2 = aux;
            aux = k1; 
            k1 = a*k1+k2; 
            k2 = aux;
            b = 1/(b-a);
        } while (Math.abs(x-h1/k1) > x*tolerance);
    
        return (int)h1+"/"+(int)k1;
    }
}

0 个答案:

没有答案