找到满足9x< 2< 10&& 7.5x< 8

时间:2016-09-04 15:49:23

标签: math dart

我正在编写一个程序,需要找到满足以下2个等式的X的最大值:

9x< 10 7.5x< 8

有人可以建议最好的方法来解决这个问题吗?我正在Dart编写该程序,但会欣赏任何语言的示例/建议。

这是我目前所做的,但我并非100%确定这是正确的:

double Xval = 10/9;
if(7.5 *  Xval > 8)
  Xval = 8 / 7.5;

请注意,如果我们更改了任何或所有数字(例如9,10,7.5或8),该程序将必须工作。

1 个答案:

答案 0 :(得分:1)

它需要一些数学逻辑。

1)首先通过将x替换为<来查找=的值。例如。在x中找到9x=10

2)找到解决方案x的最小值。

3)这个最小值将满足它的显而易见的方程式,但我们已将<替换为=,因此我们需要减去一个最小值,我们可以通过它找到最大值x满足原始方程式。

4)如果你想要4个小数点精度,那么从最小值中减去值0.0001。通常会减去(10)^(-DecimalPointPrecision)值,因此DecimalPointPrecision等于4

5)你得到的这个值将满足两个方程,它将是x的最大值。

我在java中编写了一个实现此逻辑的代码。

import java.util.Scanner;

 class SolveEquation
 {
  public static void main(String [] args)
   {
        float ip1_left;
        float ip1_right;
        float ip2_left;
        float ip2_right;

        Scanner sc=new Scanner(System.in);

        System.out.print("\nEnter the multiplier of x of 1st equation:");
        ip1_left=sc.nextFloat();

        System.out.print("Enter the constant of 1st equation:");
        ip1_right=sc.nextFloat();

        System.out.print("Enter the multiplier of x of 2nd equation:");
        ip2_left=sc.nextFloat();

        System.out.print("Enter the constant of x of 2nd equation:");
        ip2_right=sc.nextFloat();

        float ans1=ip1_right/ip1_left;
        float ans2=ip2_right/ip2_left;

        float min=ans1;

        if(ans2<ans1)
            min=ans2;
        //If you want 4 decimal precision then print 4 digits after point and subtract 0.0001 (where 1 is placed on 4th place after decimal point).
        System.out.printf("\nMaximum value of x is %.4f",min-0.0001); 



   }
 }

根据您的问题输入的示例输出: Output