Codechef:如果给定的斜边和面积可以形成直角三角形

时间:2020-03-30 08:10:13

标签: java math

问题链接:https://www.codechef.com/problems/RIGHTTRI 我不知道为什么在使用三角形的不等式属性时,代码给出了错误的答案。我不明白为什么在使用此属性时会给出错误的答案。 这是我的代码的片段:

    for(int i=0;i<t;i++)
    {
        double h=0;
        if(sc.hasNext())
        {
            h=sc.nextDouble();
        }
        double s=0;
        {
            s=sc.nextDouble();

        }
        double side1=0;
        double side2=0;
        if(h*h*h*h>=16*s*s)
        {
         side1=Math.sqrt((h*h+Math.sqrt(h*h*h*h-16*s*s))/2);
          side2=Math.sqrt((h*h-Math.sqrt(h*h*h*h-16*s*s))/2);
        }
        //to print up to given decimal digits DecimalFormat df = new DecimalFormat("###.######");
        if(side1+side2>h&&h+side2>side1) //*************problem is here I guess***************
        {
            System.out.println(df.format(side2)+" "+df.format(side1)+" "+df.format(h));
        }
        else{
            System.out.println("-1");
        }
    }

}}

在代码中引用“ *************问题在这里我猜***************”行。 当使用sqrt的必要条件(即h ^ 4> = 16s ^ 2 in if块)时,它工作正常,当我输入以下条件时,它也能正常工作:side1 == h和side2 == 0 但是三角形的边如何为零。 预先感谢您的帮助:)

0 个答案:

没有答案