VB常见20道编程代码.doc
《VB常见20道编程代码.doc》由会员分享,可在线阅读,更多相关《VB常见20道编程代码.doc(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-作者xxxx-日期xxxxVB常见20道编程代码【精品文档】哈哈哈20道编程代码1.1000以内的所有完数Private Sub Command1_Click() Dim n As Integer Dim i As Integer Dim s As Long For n = 1 To 1000 s = 1 For i = 2 To n - 1 If n Mod i = 0 Then s = s + i Next i If s = n Then Text1 = Text1 & n & If k Mod 5 = 0 Then Text1 = Text1 & vbCrLf End If Next
2、nEnd SubPrivate Sub Command2_Click() EndEnd Sub2.百元买百鸡Private Sub Command1_Click()Dim i As IntegerDim j As IntegerFor i = 0 To 20 For j = 0 To 33 If 5 * i + 3 * j + (100 - i - j) * 1 / 3 = 100 Then Text1 = Text1 & 买 & CStr(i) & 只公鸡 & CStr(j) & 只母鸡 & CStr(100 - i - j) & 只小鸡 Text1 = Text1 & Chr(13) &
3、Chr(10) End If Next jNext iEnd SubPrivate Sub Command2_Click() EndEnd Sub3. 不靠边元素和Private Sub Command1_Click()ClsRandomizeDim a(5, 5) As Integer, s%For i = 0 To 5 For j = 0 To 5 a(i, j) = Int(Rnd * 9) Print a(i, j) & ,; Sum = Sum + a(i, j) Next j PrintNext ik = SumSum = 0For i = 0 To 5Sum = Sum + a(
4、i, 0) + a(i, 5) + a(0, i) + a(5, i)Next iSum = Sum - a(0, 0) - a(5, 0) - a(5, 5) - a(0, 5)Text1 = k - SumEnd SubPrivate Sub Command2_Click() EndEnd Sub4.递归函数求斐波拉契数列Option ExplicitPrivate Sub Command1_Click() Dim a1, a2, b As Integer Dim i, n As Integer n = InputBox(显示数列的前N项) a1 = 1 For i = 1 To n b
5、= a1 + a2 Text1.SelText = b & a1 = a2 a2 = b Next iEnd SubPrivate Sub Command2_Click() EndEnd Sub5.各位数之和Private Sub Command1_Click() Dim i As String Dim j% Dim s% i = Text1 For j = 1 To Len(i) st = Mid(i, j, 1) s = s + Val(st) Next j Text2 = sEnd SubPrivate Sub Command2_Click() EndEnd Sub6.回文数Privat
6、e Sub Command1_Click() Dim x$, y$, c$, i% x = Text1 For i = 1 To Len(x) c = Mid(x, i, 1) y = c + y Next i If y = x Then Text2 = x是回文数 Else Text2 = x不是回文数 End IfEnd SubPrivate Sub Command2_Click() Text1 = Text2 = End SubPrivate Sub Command3_Click() EndEnd Sub7.级数求和Private Sub Command1_Click() Dim i A
7、s Integer, t As Long, s As Singlei = 0: s = o: t = 1Dos = s + 1 / ti = i + 1t = t * iText1 = sEnd SubPrivate Sub Command2_Click() EndEnd Sub8.矩阵的转置Option ExplicitOption Base 1Dim i As IntegerDim j As IntegerDim a() As IntegerDim n As IntegerPrivate Sub Command1_Click()n = Val(Text3)ReDim a(n, n) 动态数
8、组For i = 1 To n For j = 1 To n a(i, j) = Int(10 * Rnd + 0) 随机数 Text1 = Text1 & a(i, j) & 一行 Next j Text1 = Text1 & Chr(13) & Chr(10) 多行Next iEnd SubPrivate Sub Command2_Click()Dim b() As Integern = Val(Text3)ReDim b(n, n)For i = 1 To n For j = 1 To n b(i, j) = a(j, i) 转置 Text2 = Text2 & b(i, j) & 一行
9、 Next j Text2 = Text2 & Chr(13) & Chr(10) 多行Next iEnd SubPrivate Sub Command3_Click()Text1 = Text2 = Text3 = 清除End SubPrivate Sub Command4_Click()End 结束End SubPrivate Sub Form_Load()Text1 = Text2 = Text3 = 清除End Sub9.靠边元素和Private Sub Command1_Click()ClsRandomizeDim a(5, 5) As IntegerFor i = 0 To 5 F
10、or j = 0 To 5 a(i, j) = Int(Rnd * 9) Print a(i, j) & ,; Sum = Sum + a(i, j) Next j PrintNext iSum = 0For i = 0 To 5Sum = Sum + a(i, 0) + a(i, 5) + a(0, i) + a(5, i)NextSum = Sum - a(0, 0) - a(5, 0) - a(5, 5) - a(0, 5)Text1 = SumEnd SubPrivate Sub Command2_Click() EndEnd Sub10.冒泡排序Option ExplicitOpti
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VB 常见 20 编程 代码
限制150内