在VB6中求解线性方程

时间:2019-02-14 02:18:13

标签: vb6

我必须找出方程ax + by = c的积分解,以使所有变量都大于0并且(x + y)的值最小。我有C ++实现.. vb6中需要它

void compute0(int &x,int &y,int a,int b,int c) // naive
{
    int xx,yy;
    xx = -1; yy = -1;
    for (y = 0;;y++)
    {
        x = c - b*y;
        if (x < 0) break; // y out of range stop
        if (x % a) continue; // non integer solution
        x /= a; // remember minimal solution
        if ((xx < 0) || (x + y <= xx + yy))
        { 
            xx=x; yy=y; 
        } 
    }
    x=xx; y=yy;
}

1 个答案:

答案 0 :(得分:0)

这应该为您解决问题

Private Sub compute0(x As Long, y As Long, ByVal a As Long, ByVal b As Long, ByVal c As Long) 'naive
Dim xx          As Long
Dim yy          As Long
Dim continue    As Boolean

xx = -1
yy = -1
y = 0
continue = True

Do While continue
    x = c - b * y
    If (x < 0) Then 'y out of range stop
        Exit Do
    End If

    If (x Mod a) = 0 Then
        x = x / a 'remember minimal solution
        If ((xx < 0) Or (x + y <= xx + yy)) Then
            xx = x
            yy = y
        End If
    End If
    y = y + 1
Loop

x = xx
y = yy
End Sub

使用前请确保已对其进行测试。