我需要计算这样的公式,
X1*1.5+X2*1.1+X3*6.1+X4*10.2+X5*14.3+X6*20.6+X7*4.8+X8*5.1+X9*7.4+X10*3.9=5000
不知道怎么写。请高手指点一二。

解决方案 »

  1.   

    //希望对你有帮助。
    我感觉用数组应该可以实现。
    如果是double类型的,array of double,如果是公式的话,写成函数,以方便调用。
      

  2.   

    实际上我的这个方程式是我想求x1 -x10这几个未知数。X1*a1+x2*a2...x10*a10=5000 a1-a10是固定的但是都是有小数部分的。有个朋友给个程序,但是不能计算小数部分,求助那位朋友给个解决办法。
    for x1 = 1 to (5000 div 15) do 
      for x2 =1 to (5000 div 11) do 
        ,,,
       for x10 =1 to (5000 div 3) do 
       if (x1*15)+(x2*11)+...+(x10*3)=5000 then 
        輸出 x1,x2,..x10
      

  3.   

    X1*a1+x2*a2...x10*a10和5000的之间差0.5以内。
      

  4.   

    我写的代码。但是算不出来。大家帮忙看看那里写的不对。
    procedure Tmain.Button1Click(Sender: TObject);
    var
       a1,a2,a3,a4,a5,a6,a7,a8,a9,a10:double;
       b1,b2,b3,b4,b5,b6,b7,b8,b9,b10:integer;
       x1,x2,x3,x4,x5,x6,x7,x8,x9,x10:integer;
       sum1:double;
    begin
          sum1:=strtofloat(trim(MaskEdit61.Text));      a1:=strtofloat(trim(MaskEdit11.Text));
          a2:=strtofloat(trim(MaskEdit12.Text));
          a3:=strtofloat(trim(MaskEdit13.Text));
          a4:=strtofloat(trim(MaskEdit14.Text));
          a5:=strtofloat(trim(MaskEdit15.Text));
          a6:=strtofloat(trim(MaskEdit16.Text));
          a7:=strtofloat(trim(MaskEdit17.Text));
          a8:=strtofloat(trim(MaskEdit18.Text));
          a9:=strtofloat(trim(MaskEdit19.Text));
          a10:=strtofloat(trim(MaskEdit20.Text));      b1:=trunc(sum1/a1);
          b2:=trunc(sum1/a2);
          b3:=trunc(sum1/a3);
          b4:=trunc(sum1/a4);
          b5:=trunc(sum1/a5);
          b6:=trunc(sum1/a6);
          b7:=trunc(sum1/a7);
          b8:=trunc(sum1/a8);
          b9:=trunc(sum1/a9);
          b10:=trunc(sum1/a10);
          //---------------------------------------------------------
          for x1:=1 to b1 do
           for x2:=1 to b2 do
             for x3:=1 to b3 do
               for x4:=1 to b4 do
                 for x5:=1 to b5 do
                   for x6:=1 to b6 do
                     for x7:=1 to b7 do
                       for x8:=1 to b8 do
                         for x9:=1 to b9 do
                           for x10:=1 to b10 do
                     if (x1*a1)+(x2*a2)+(x3*a3)+(x4*a4)+(x5*a5)+(x6*a6)+(x7*a7)+(x8*a8)+(x9*a9)+(x10*a10)<=sum1 then
                       begin
                         if (x1*a1)+(x2*a2)+(x3*a3)+(x4*a4)+(x5*a5)+(x6*a6)+(x7*a7)+(x8*a8)+(x9*a9)+(x10*a10)>=(sum1-0.5) then
                             begin
                                MaskEdit62.Text:=floattostr((x1*a1)+(x2*a2)+(x3*a3)+(x4*a4)+(x5*a5)+(x6*a6)+(x7*a7)+(x8*a8)+(x9*a9)+(x10*a10));
                             end;
                         end;
    end;end.
      

  5.   

    //delphi 7下,测试通过,按照楼主的方法,是能通过的,只不过要在最后那句,加一个exit,否则就运算到底了,没有退出条件了。
    //按照楼主给出的10个值,运算的结果是4999.9procedure TForm1.Button2Click(Sender: TObject);
    var
      a1,a2,a3,a4,a5,a6,a7,a8,a9,a10:double;
      b1,b2,b3,b4,b5,b6,b7,b8,b9,b10:integer;
      x1,x2,x3,x4,x5,x6,x7,x8,x9,x10:integer;
      sum1:double;
    begin
      sum1:=strtofloat(trim(edtsum.Text));  a1:=strtofloat(trim(edt1.Text));
      a2:=strtofloat(trim(edt2.Text));
      a3:=strtofloat(trim(edt3.Text));
      a4:=strtofloat(trim(edt4.Text));
      a5:=strtofloat(trim(edt5.Text));
      a6:=strtofloat(trim(edt6.Text));
      a7:=strtofloat(trim(edt7.Text));
      a8:=strtofloat(trim(edt8.Text));
      a9:=strtofloat(trim(edt9.Text));
      a10:=strtofloat(trim(edt10.Text));  b1:=trunc(sum1/a1);
      b2:=trunc(sum1/a2);
      b3:=trunc(sum1/a3);
      b4:=trunc(sum1/a4);
      b5:=trunc(sum1/a5);
      b6:=trunc(sum1/a6);
      b7:=trunc(sum1/a7);
      b8:=trunc(sum1/a8);
      b9:=trunc(sum1/a9);
      b10:=trunc(sum1/a10);
      //---------------------------------------------------------
      for x1:=1 to b1 do
      begin
        for x2:=1 to b2 do
        begin
          for x3:=1 to b3 do
          begin
            for x4:=1 to b4 do
            begin
              for x5:=1 to b5 do
              begin
                for x6:=1 to b6 do
                begin
                  for x7:=1 to b7 do
                  begin
                    for x8:=1 to b8 do
                    begin
                      for x9:=1 to b9 do
                      begin
                        for x10:=1 to b10 do
                        begin
                          if (x1*a1)+(x2*a2)+(x3*a3)+(x4*a4)+(x5*a5)+(x6*a6)+(x7*a7)+(x8*a8)+(x9*a9)+(x10*a10) <=sum1 then
                          begin
                            if (x1*a1)+(x2*a2)+(x3*a3)+(x4*a4)+(x5*a5)+(x6*a6)+(x7*a7)+(x8*a8)+(x9*a9)+(x10*a10)>=(sum1-0.5) then
                            begin
                              self.addText('运算成功!');
                              self.addText(floattostr((x1*a1)+(x2*a2)+(x3*a3)+(x4*a4)+(x5*a5)+(x6*a6)+(x7*a7)+(x8*a8)+(x9*a9)+(x10*a10)));
                              exit;
                            end;
                          end;  
                        end; 
                      end;  
                    end;   
                  end;
                end;        
              end;
            end;
          end;
        end;            
      end;              
    end;
      

  6.   

    我实验了。能通过,但是没有合适条件就会死机。没有找到在哪里退出。
    procedure Tmain.Button1Click(Sender: TObject);
    var
       a1,a2,a3,a4,a5,a6,a7,a8,a9,a10:double;
       b1,b2,b3,b4,b5,b6,b7,b8,b9,b10:integer;
       c1,c2,c3,c4,c5,c6,c7,c8,c9,c10:integer;
       x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,o:integer;
       sum1:double;
    begin
          sum1:=strtofloat(trim(MaskEdit61.Text));
          if sum1=0 then
             begin
                  application.MessageBox('目标金额不能为空!','系统提示',mb_ok);
                  MaskEdit61.SetFocus;
                  exit;
             end;
          a1:=strtofloat(trim(MaskEdit11.Text));
          a2:=strtofloat(trim(MaskEdit12.Text));
          a3:=strtofloat(trim(MaskEdit13.Text));
          a4:=strtofloat(trim(MaskEdit14.Text));
          a5:=strtofloat(trim(MaskEdit15.Text));
          a6:=strtofloat(trim(MaskEdit16.Text));
          a7:=strtofloat(trim(MaskEdit17.Text));
          a8:=strtofloat(trim(MaskEdit18.Text));
          a9:=strtofloat(trim(MaskEdit19.Text));
          a10:=strtofloat(trim(MaskEdit20.Text));      b1:=strtoint(trim(MaskEdit41.Text));
          b2:=strtoint(trim(MaskEdit42.Text));
          b3:=strtoint(trim(MaskEdit43.Text));
          b4:=strtoint(trim(MaskEdit44.Text));
          b5:=strtoint(trim(MaskEdit45.Text));
          b6:=strtoint(trim(MaskEdit46.Text));
          b7:=strtoint(trim(MaskEdit47.Text));
          b8:=strtoint(trim(MaskEdit48.Text)); 
          b9:=strtoint(trim(MaskEdit49.Text));
          b10:=strtoint(trim(MaskEdit50.Text));
          //---------------------------------------------------------
          for x1:=c1 to b1 do
          for x2:=c2 to b2 do
          for x3:=c3 to b3 do
          for x4:=c4 to b4 do
          for x5:=c5 to b5 do
          for x6:=c6 to b6 do
          for x7:=c7 to b7 do
          for x8:=c8 to b8 do
          for x9:=c9 to b9 do
          for x10:=c10 to b10 do
          if (x1*a1)+(x2*a2)+(x3*a3)+(x4*a4)+(x5*a5)+(x6*a6)+(x7*a7)+(x8*a8)+(x9*a9)+(x10*a10)<=sum1 then
             begin
               if (x1*a1)+(x2*a2)+(x3*a3)+(x4*a4)+(x5*a5)+(x6*a6)+(x7*a7)+(x8*a8)+(x9*a9)+(x10*a10)>=(sum1-0.5) then
                  begin
                     MaskEdit62.Text:=floattostr((x1*a1)+(x2*a2)+(x3*a3)+(x4*a4)+(x5*a5)+(x6*a6)+(x7*a7)+(x8*a8)+(x9*a9)+(x10*a10));                 
                     application.MessageBox('完成!','系统提示',mb_ok);
                     exit;
                  end;
          end;
    end;