‘ 【程序2】
‘題目:一個(gè)整數(shù),它加上100后是一個(gè)完全平方數(shù),再加上168又是一個(gè)完全平方數(shù),請(qǐng)問(wèn)該數(shù)是多少?
Sub prog2()
‘
Dim jilu(100), strjilu
j = 1
For i = 1 To 100
jilu(i) = 0
Next i
For i = 1 To 10000
If Sqr(i + 100) = Int(Sqr(i + 100)) Then
If Sqr(i + 268) = Int(Sqr(i + 268)) Then
jilu(j) = i
j = j + 1
‘MsgBox "該數(shù)為" & CStr(i)
‘Exit For
End If
End If
Next
If jilu(1) = 0 Then
MsgBox "meiyou"
Else
For i = 1 To 100
If jilu(i) <> 0 Then
strjilu = strjilu + "||" + CStr(jilu(i))
End If
Next
MsgBox "該數(shù)為" & strjilu
End If
End Sub
【程序3】
‘題目:輸入兩個(gè)正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)。
Sub prog3()
‘
Dim m, n
m = 30: n = 14
If m = n Then
beishu = m
yueshu = m
ElseIf m > n Then
For i = n To 1 Step -1
If (m Mod i = 0) And (n Mod i = 0) Then
yueshu = i
Exit For
End If
Next
j = m
tt = 0
Do While (tt = 0)
j = j + 1
If (j Mod m = 0) And (j Mod n = 0) Then
beishu = j
tt = 1
End If
Loop
End If
MsgBox "beishu" & CStr(beishu) & " " & "yueshu" & CStr(yueshu)
End Sub
‘【程序4】
‘題目:一個(gè)數(shù)如果恰好等于它的因子之和,這個(gè)數(shù)就稱為“完數(shù)”。例如6=1+2+3.編程找出100以內(nèi)的所有完數(shù)?
‘對(duì)n進(jìn)行分解質(zhì)因數(shù),應(yīng)先找到一個(gè)最小的質(zhì)數(shù)k,然后按下述步驟完成:
‘(1)如果這個(gè)質(zhì)數(shù)恰等于n,則說(shuō)明分解質(zhì)因數(shù)的過(guò)程已經(jīng)結(jié)束,打印出即可。
‘(2)如果n<>k,但n能被k整除,則應(yīng)打印出k的值,并用n除以k的商,作為新的正整數(shù)n,重復(fù)執(zhí)行第一步
‘(3)如果n不能被k整除,則用k+1作為k的值,重復(fù)執(zhí)行第一步。
Sub prog4()
‘
Dim a(20)
n = 1
m = 1
For i = 1 To 100
‘
k = 1
a(1) = 1
For j = 2 To 20
a(j) = 0
Next
l = i
pp = True
‘尋找數(shù)的因子
Do While pp
If l = 1 Then
Exit Do
End If
For j = 2 To l
If l Mod j = 0 Then
a(k) = j
k = k + 1
l = l / j
Exit For
End If
Next
Loop
‘求各因子的和
s = 0
For j = 1 To 20
s = s + a(j)
Next
‘判斷是否相等
If (s + 1) = i Then
Sheet2.Cells(n, 3) = i
n = n + 1
‘Exit For
End If
‘
For j = 1 To 20
If a(j) <> 0 Then
Sheet2.Cells(m, 4) = 1
Sheet2.Cells(m, j + 4) = a(j)
Else
Exit For
End If
Next
m = m + 1
Next
End Sub
【程序5】
‘題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地時(shí),共經(jīng)過(guò)多少米?第10次反彈多高?
Sub prog5()
‘
Dim fantai(10)
fantai(1) = 100
For i = 2 To 10
fantai(i) = fantai(i - 1) / 2
Next
s = 0
For i = 1 To 10
s = s + fantai(i)
Next
MsgBox "共經(jīng)過(guò)" & CStr(s) & "米;第10次反彈" & CStr(fantai(10))
End Sub
‘【程序6】
‘題目:猴子吃桃問(wèn)題:猴子第一天摘下若干個(gè)桃子,當(dāng)即吃了一半,還不過(guò)癮,又多吃了一個(gè)第二天早上又將剩下的桃子吃掉一半,
‘又多吃了一個(gè)。以后每天早上都吃了前一天剩下的一半零一個(gè)。到第10天早上想再吃時(shí),見(jiàn)只剩下一個(gè)桃子了。求第一天共摘了多少。
Sub prog6()
‘
Dim taozi(10)
For i = 10 To 1 Step -1
If i = 10 Then
taozi(i) = 1
Else
taozi(i) = (taozi(i + 1) + 1) * 2
End If
Next
MsgBox CStr(taozi(1))
End Sub
【程序7】
‘題目:編寫(xiě)一個(gè)函數(shù),輸入n為偶數(shù)時(shí),調(diào)用函數(shù)求1/2+1/4+...+1/n,當(dāng)輸入n為奇數(shù)時(shí),調(diào)用函數(shù)
‘1/1+1/3+...+1/n(利用指針函數(shù))
Sub prog7()
‘
n = 12
s = 0
If n Mod 2 = 0 Then
For i = 2 To n Step 2
s = s + 1 / i
Next
Else
For i = 1 To n Step 2
s = s + 1 / i
Next
End If
MsgBox CStr(s)
End Sub
‘【程序8】
‘題目:海灘上有一堆桃子,五只猴子來(lái)分。第一只猴子把這堆桃子憑據(jù)分為五份,多了一個(gè),這只
‘猴子把多的一個(gè)扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了
‘一個(gè),它同樣把多的一個(gè)扔入海中,拿走了一份,第三、第四、第五只猴子都是這樣做的,
‘問(wèn)海灘上原來(lái)最少有多少個(gè)桃子?
Sub prog8()
‘
j = 1
For i = 1 To 10000
If (i - 1) Mod 5 = 0 Then ‘1只猴子分
t1 = (i - 1) * 4 / 5
If (t1 - 1) Mod 5 = 0 Then ‘2只猴子分
t2 = (t1 - 1) * 4 / 5
If (t2 - 1) Mod 5 = 0 Then ‘3只猴子分
t3 = (t2 - 1) * 4 / 5
If (t3 - 1) Mod 5 = 0 Then ‘4只猴子分
t4 = (t3 - 1) * 4 / 5
If (t4 - 1) Mod 5 = 0 Then ‘5只猴子分
Sheet1.Cells(j, 1) = i
Sheet1.Cells(j, 2) = t1 / 4
Sheet1.Cells(j, 3) = t2 / 4
Sheet1.Cells(j, 4) = t3 / 4
Sheet1.Cells(j, 5) = t4 / 4
Sheet1.Cells(j, 6) = (t4 - 1) / 5
j = j + 1
‘If j = 21 Then Exit For
‘MsgBox "taozhi" & CStr(i)
‘Exit For
End If
End If
End If
End If
End If
Next
End Sub
【程序9】
‘題目:打印出所有的“水仙花數(shù)”,所謂“水仙花數(shù)”是指一個(gè)三位數(shù),其各位數(shù)字立方和等于該數(shù)
‘本身。例如:153是一個(gè)“水仙花數(shù)”,因?yàn)?53=1的三次方+5的三次方+3的三次方。
Sub prog9()
‘
m = 1
For i = 100 To 999
j = Int(i / 100)
k = Int((i - j * 100) / 10)
l = i - 100 * j - k * 10
If i = j ^ 3 + k ^ 3 + l ^ 3 Then
Sheet1.Cells(m, 8) = i
m = m + 1
End If
Next
End Sub
‘【程序10】
‘題目:求1+2!+3!+...+20!的和
Sub prog10()
‘
s = 0
For i = 1 To 20
t = 1
For j = 1 To i
t = t * j
Next
s = s + t
Next
MsgBox CStr(s)
End Sub
【程序11】
‘題目:一個(gè)5位數(shù),判斷它是不是回文數(shù)。即12321是回文數(shù),個(gè)位與萬(wàn)位相同,十位與千位相同。
Sub prog11()
‘
j = 1
For i = 10000 To 99999
k = Int(i / 10000)
l = Int((i - k * 10000) / 1000)
m = CInt(Left((Right(CStr(i), 2)), 1))
n = CInt((Right(CStr(i), 1)))
If (k = n) And (l = m) Then
Sheet2.Cells(j, 1) = i
j = j + 1
End If
Next
End Sub
‘【程序12】
‘題目:判斷101-200之間有多少個(gè)素?cái)?shù),并輸出所有素?cái)?shù)。
Sub prog12()
‘
k = 1
For i = 101 To 200
For j = 2 To Int(Sqr(i)) + 1
If i Mod j = 0 Then
Exit For
End If
Next
If j = Int(Sqr(i)) + 2 Then
Sheet2.Cells(k, 2) = i
k = k + 1
End If
Next
End Sub
【程序13】
‘題目:有1、2、3、4個(gè)數(shù)字,能組成多少個(gè)互不相同且無(wú)重復(fù)數(shù)字的三位數(shù)?都是多少?
Sub prog13()
‘
Dim a(4)
m = 0
‘m,n,o
For i = 1 To 4
a(i) = i
Next
For i = 1 To 4
For j = 1 To 4
For k = 1 To 4
If ((i <> k) And (i <> j)) And (j <> k) Then
m = m + 1
Sheet3.Cells(1, m + 1) = CStr(i) + CStr(j) + CStr(k)
End If
Next
Next
Next
Sheet3.Cells(1, 1) = m
End Sub
‘【程序14】
‘題目:企業(yè)發(fā)放的獎(jiǎng)金根據(jù)利潤(rùn)提成。利潤(rùn)(I)低于或等于10萬(wàn)元時(shí),獎(jiǎng)金可提10%;利潤(rùn)高
‘于10萬(wàn)元,低于20萬(wàn)元時(shí),低于10萬(wàn)元的部分按10%提成,高于10萬(wàn)元的部分,可可提
‘成7.5%;20萬(wàn)到40萬(wàn)之間時(shí),高于20萬(wàn)元的部分,可提成5%;40萬(wàn)到60萬(wàn)之間時(shí)高于
‘40萬(wàn)元的部分,可提成3%;60萬(wàn)到100萬(wàn)之間時(shí),高于60萬(wàn)元的部分,可提成1.5%,高于
‘100萬(wàn)元時(shí),超過(guò)100萬(wàn)元的部分按1%提成,在sheet3(2,1)中輸入月利潤(rùn)I,在sheet3(2,2)中求出發(fā)放獎(jiǎng)金總數(shù)。
Sub prog14()
‘
a1 = 0.1 ‘i<=10
a2 = 0.075 ‘10<i<=20
a3 = 0.05 ‘20<i<=40
a4 = 0.03 ‘40<i<=60
a5 = 0.015 ‘60<i<=100
a6 = 0.01 ‘100<i
i = Sheet3.Cells(2, 1)
bouns = 0
pp = True
Do While pp
If i > 100 Then
bouns = bouns + (i - 100) * a6
i = i - 100
ElseIf i > 60 And i <= 100 Then
bouns = bouns + (i - 60) * a5
i = i - 60
ElseIf i > 40 And i <= 60 Then
bouns = bouns + (i - 40) * a4
i = i - 40
ElseIf i > 20 And i <= 40 Then
bouns = bouns + (i - 20) * a3
i = i - 20
ElseIf i > 10 And i <= 20 Then
bouns = bouns + (i - 10) * a2
i = i - 10
ElseIf i <= 10 Then
bouns = bouns + i * a1
Exit Do
End If
Loop
Sheet3.Cells(2, 2) = bouns
End Sub
在sheet3的代碼輸入處輸入如下代碼:
(我在調(diào)試時(shí)將所有的程序都放在sheet1中)
Private Sub Worksheet_Change(ByVal Target As Range)
‘
If Target = Cells(2, 1) Then
Sheet1.prog14
End If
End Sub
‘【程序15】
‘題目:有一分?jǐn)?shù)序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個(gè)數(shù)列的前20項(xiàng)之和。
Sub prog15()
‘
Dim a(20), b(20)
a(1) = 2
b(1) = 1
s = a(1) / b(1)
For i = 2 To 20
a(i) = a(i - 1) + b(i - 1)
b(i) = a(i - 1)
s = s + a(i) / b(i)
Next
MsgBox CStr(s)
End Sub