VB程序,求鸡兔同笼问题

来源:百度知道 编辑:UC知道 时间:2024/06/29 01:06:11
Dim x As Integer, y, h, f As Integer

h = InputBox("enter 总头数h")
f = InputBox("enter 总脚数f")
Print "x="; (f - 2 * h) / 2; "y="; (4 * h - f) / 2
我这么做为什么不对呢?非常搞笑的事,输入脚没头多的时候仍能算出数来

Private Sub Command1_Click()
Dim x As Integer, y, h, f As Integer
h = InputBox("enter 总头数h")
f = InputBox("enter 总脚数f")
If f >= 2 * h Then
Print "x="; (f - 2 * h) / 2; "y="; (4 * h - f) / 2
Else
Print "Error"
End If
End Sub

脚至少是头的2倍吧,加个限制条件

Option Explicit

Private Sub command1_Click()
Dim lngNumA As Long, lngNumB As Long, lngTmp As Long
lngNumA = 0: lngNumB = 0
Do
lngNumB = InputIntegerNumBox("请输入鸡和兔的下肢之和", "Fir Says:")
If lngNumB Mod 2 Then lngNumB = 0
Loop Until lngNumB > 0
Do
lngNumA = InputIntegerNumBox("请输入鸡和兔的脑袋之和", "Fir Says:")
If lngNumA * 2 >= lngNumB Then lngNumA = 0
Loop Until lngNumA > 0
Dim lngIndex As Long
For lngIndex = 0 To lngNumA
lngTmp = lngNumA - lngIndex