h个和尚h个馒头,一大和尚能吃四个馒头,四小和尚能吃一个馒头,求大小和尚人数,画出框图写出编程。
发布网友
发布时间:2022-11-28 06:08
我来回答
共2个回答
热心网友
时间:2023-10-21 14:17
您好,
最终效果:
以下是 VB 代码的实现(做了高度封装):
frmMonk.frm
Option Explicit
Dim WithEvents mk As Monk
Rem 题目分析
Rem 设大和尚为人数为 X,小和尚为人数为 Y,馒头数为 Z,根据题意,则有方程组:
Rem X + Y = Z
Rem 4 * X + Y / 4 = Z
Rem 把第一个式子中 Z 的值代入到第二个式子中,则有
Rem X + Y = 4 * X + Y / 4
Rem 两边同时乘以 4,得到
Rem 4 * X + 4 * Y = 16 * X + Y
Rem 12 * X = 3 * Y
Rem 等式两边同时除以 3,得到
Rem 4 * X = Y
Rem 这时就可以得到大和尚人数和小和尚人数的最终关系了,也就是小和尚人数是大和尚人数的四倍!
Private Sub cboLimitedOption_Click()
txtLimited.SetFocus
End Sub
Private Sub cmdEnum_Click()
If lstOutput.ListCount > 0 Then lstOutput.Clear
If cboLimitedOption.ListIndex <> -1 Then
On Error GoTo ERR_EXCEPTION
If Trim$(txtLimited.Text) <> vbNullString Then
Call mk.OutputResultLimitedByNumberOfMonk_L(cboLimitedOption.ListIndex, CInt(txtLimited.Text))
If mk.HasError Then MsgBox "执行期间遇到错误!", vbCritical, "错误"
Else
MsgBox "请输入*数", vbExclamation, "提示"
txtLimited.SetFocus
End If
Else
MsgBox "请选择*类型", vbExclamation, "提示"
End If
ERR_EXIT:
With mk
.NumberOfMonk_L = 1
.NumberOfMonk_S = 4
End With
Exit Sub
ERR_EXCEPTION:
MsgBox Err.Description, vbCritical, "错误"
txtLimited.SelStart = 0
txtLimited.SelLength = Len(txtLimited.Text)
txtLimited.SetFocus
Resume ERR_EXIT
End Sub
Private Sub Form_Load()
Set mk = New Monk
With cboLimitedOption
.Text = "*类型"
.AddItem "大和尚人数"
.AddItem "小和尚人数"
.AddItem "和尚总人数"
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not (mk Is Nothing) Then Set mk = Nothing
End Sub
Private Sub mk_OutputChange(ByVal intNumberOfMonk_L As Integer, ByVal intNumberOfMonk_S As Integer)
lstOutput.AddItem "大和尚人数为: " & CStr(intNumberOfMonk_L) & " " & vbTab & "小和尚人数为: " & " " & CStr(intNumberOfMonk_S) & vbTab & "和尚总人数为: " & CStr(mk.NumberOfMonk_A)
End Sub
Monk.cls
Option Explicit
' ---------------
' 枚举: 输出选项.
' ---------------
Public Enum OutputOption
ByNumberOfMonk_L ' 大和尚人数.
ByNumberOfMonk_S ' 小和尚人数.
ByNumberOfMonk_A ' 和尚总人数.
End Enum
' -------------------
' 事件: 输出改变事件.
' -------------------
Public Event OutputChange(ByVal intNumberOfMonk_L As Integer, ByVal intNumberOfMonk_S As Integer)
' ---------------------
' 保持属性值的局部变量.
' ---------------------
Private mintNumberOfMonk_L As Integer ' 大和尚人数.
Private mintNumberOfMonk_S As Integer ' 小和尚人数.
Private mintNumberOfMonk_A As Integer ' 和尚总人数.
Private mblnHasError As Boolean ' 有错误.
' -------------------------
' 属性: 大和尚人数 (读/写).
' -------------------------
Public Property Let NumberOfMonk_L(ByVal intNumberOfMonk_L As Integer)
mintNumberOfMonk_L = intNumberOfMonk_L
End Property
Public Property Get NumberOfMonk_L() As Integer
NumberOfMonk_L = mintNumberOfMonk_L
End Property
' -------------------------
' 属性: 小和尚人数 (读/写).
' -------------------------
Public Property Let NumberOfMonk_S(ByVal intNumberOfMonk_S As Integer)
mintNumberOfMonk_S = intNumberOfMonk_S
End Property
Public Property Get NumberOfMonk_S() As Integer
NumberOfMonk_S = mintNumberOfMonk_S
End Property
' -------------------------
' 属性: 和尚总人数 (只读).
' -------------------------
Public Property Get NumberOfMonk_A() As Integer
On Error GoTo ERR_RESET
NumberOfMonk_A = mintNumberOfMonk_L + mintNumberOfMonk_S
Exit Property
ERR_RESET:
mblnHasError = True
End Property
' ------------------------------------
' 属性: 指示运行期间是否有错误 (只读).
' ------------------------------------
Public Property Get HasError() As Boolean
HasError = mblnHasError
End Property
' ########################################
' [功能描述] 以和尚的目标人数*输出结果.
' [参数列表] 1. LimitedOption *选项;
' 2. TargetNumber 目标人数.
' [返回类型] 无.
' ########################################
Public Sub OutputResultLimitedByNumberOfMonk_L(ByVal LimitedOption As OutputOption, ByVal TargetNumber As Integer)
If mblnHasError Then mblnHasError = False
On Error GoTo ERR_RESET
Select Case LimitedOption
Case ByNumberOfMonk_A
Do
mintNumberOfMonk_S = 4 * mintNumberOfMonk_L
If mintNumberOfMonk_L + mintNumberOfMonk_S > TargetNumber Then Exit Do
RaiseEvent OutputChange(mintNumberOfMonk_L, mintNumberOfMonk_S)
mintNumberOfMonk_L = mintNumberOfMonk_L + 1
Loop
Case ByNumberOfMonk_L
Do
mintNumberOfMonk_S = 4 * mintNumberOfMonk_L
If mintNumberOfMonk_L > TargetNumber Then Exit Do
RaiseEvent OutputChange(mintNumberOfMonk_L, mintNumberOfMonk_S)
mintNumberOfMonk_L = mintNumberOfMonk_L + 1
Loop
Case ByNumberOfMonk_S
Do
mintNumberOfMonk_S = 4 * mintNumberOfMonk_L
If mintNumberOfMonk_S > TargetNumber Then Exit Do
RaiseEvent OutputChange(mintNumberOfMonk_L, mintNumberOfMonk_S)
mintNumberOfMonk_L = mintNumberOfMonk_L + 1
Loop
End Select
Exit Sub
ERR_RESET:
mblnHasError = True
End Sub
' #########
' 构造函数.
' #########
Private Sub Class_Initialize()
mblnHasError = False
mintNumberOfMonk_L = 1
mintNumberOfMonk_S = 4
End Sub
热心网友
时间:2023-10-21 14:18
因为你的前两个数知不知道 ,但全都让你写成了相同的小h代表,也许是我的电脑有问题,所以改一下
和尚的数改为H
而馒头数为h
设大和尚有x人,因为大小和尚一共有h人,所以小和尚有(和尚总数H-x)人
可以列个等量关系:大和尚吃的馒头为 4*x 小和尚吃的馒头为1/4 *(H-x)
因为大和尚吃的馒头+小和尚吃的馒头=h
所以4*x+1/4*(H-x)=h