资源描述
1级数数列求和
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim m, i, n As Integer
Dim s, j As Double
n = InputBox("请输入n的值" & n)
i = 0
m = 1
s = 2
For i = 1 To n
i = i + 1
m = m + i
j = 1 + 1 / i
s = s + j
Next
MsgBox(Str(s))
End Sub
End Class
2求回文数
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, ww, qw, sw, gw, k As Integer
Dim str1 As String
str1 = ""
For i = 10000 To 50000
ww = i / 10000
qw = (i Mod 10000) / 1000
sw = (i Mod 100) / 10
gw = i Mod 10
If ww = gw And qw = sw Then
k = k + 1
If k Mod 5 = 0 Then
str1 = str1 + Str(i) + vbCrLf
Else : str1 = str1 + Str(i)
End If
End If
Next
MsgBox(Str(k) + vbCrLf + str1)
End Sub
End Class
3求素数
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, j, k, s As Integer
Dim str1 As String
str1 = ""
For i = 3 To 100
For j = 2 To i - 1
If i Mod j = 0 Then
Exit For
End If
Next j
If j = i Then
k = k + 1
If k Mod 5 = 0 Then
str1 = str1 + Str(i) + vbCrLf
Else : str1 = str1 + Str(i)
End If
End If
Next
MsgBox(str1)
End Sub
End Class
求完全数
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, j, k, s As Integer
Dim str1 As String
str1 = ""
For i = 2 To 10000
s = 0
For j = 1 To i / 2
If i Mod j = 0 Then
s = s + j
End If
Next
If i = s Then
k = k + 1
If i Mod 5 = 0 Then
str1 = str1 + Str(i) + vbCrLf
Else : str1 = str1 + Str(i)
End If
End If
Next
MsgBox("k=" + k.ToString() + vbCrLf + str1)
End Sub
End Class
矩阵最大元素查找
Dim a(,), n, m, i, j As Integer
Dim max, row, col As Integer
Dim str1 As String
n = Val(InputBox("请输入矩阵行数n="))
m = Val(InputBox("请输入矩阵列数m="))
ReDim a(n - 1, m - 1)
For i = 0 To n - 1
For j = 0 To j - 1
a(i, j) = Val(InputBox("请输入" & i & "行" & j & "列元素的值"))
str1 = str1 + Str(a(i, j)) + Space(4)
Next j
str1 = str1 + Chr(10) + Chr(13)
Next i
max = a(0, 0)
For i = 0 To n - 1
For j = 0 To m - 1
If a(i, j) > max Then
max = a(i, j)
row = i
col = j
End If
Next j
Next i
TextBox1.Text = str1
TextBox2.Text = max
TextBox3.Text = row
TextBox4.Text = col
2、习题7-7 矩阵转置
Dim a(,), b(,), n, m, i, j As Integer
Dim str1, str2 As String
n = Val(InputBox("请输入矩阵行数n="))
m = Val(InputBox("请输入矩阵列数m="))
ReDim a(n - 1, m - 1)
ReDim b(m - 1, n - 1)
For i = 0 To n - 1
For j = 0 To m - 1
a(i, j) = Val(InputBox("请输入" & i & "行" & j & "列元素的值"))
str1 = str1 + Str(a(i, j)) + Space(4)
Next j
str1 = str1 + Chr(13) + Chr(10)
Next i
For i = 0 To m - 1
For j = 0 To n - 1
b(i, j) = a(j, i)
str2 = str2 + Str(b(i, j)) + Space(4)
Next j
str2 = str2 + Chr(13) + Chr(10)
Next i
TextBox1.Text = str1
TextBox2.Text = str2
3、习题7-8 矩阵输入/输出,行/列求和,元素对换
Dim a(3, 3), i, j As Integer
Dim str1, str2 As String
For i = 0 To 3
For j = 0 To 3
a(i, j) = Val(InputBox("请输入" & i & "行" & j & "列元素的值"))
If i = j And i + j = 3 Then
str1 = str1 + Str(a(i, j))
End If
Next
Next
MsgBox(str1)
Dim s1, s2 As Integer
Dim a(3, 3), i, j As Integer
s1 = s2 = 0
For i = 0 To 3
For j = 0 To 3
a(i, j) = Val(InputBox("请输入" & i & "行" & j & "列元素的值"))
s1 = a(0, j) + s1
s2 = s2 + a(i, 0)
Next
Next
MsgBox(s1)
TextBox1.Text = s2
Dim t As Integer
Dim a(3, 3), i, j As Integer
Dim str1 As String
For i = 0 To 3
For j = 0 To 3
a(i, j) = Val(InputBox("请输入" & i & "行" & j & "列元素的值"))
t = a(0, j)
a(2, j) = t
str1 = str1 + Str(a(i, j))
Next
Next
4、习题7-12 输出“杨辉三角形”
Dim a(,), n, m, i, j As Integer
Dim str1 As String
ReDim a(11, 11)
For i = 1 To 11
For j = 1 To i
If i = j Then
a(i, j) = 1
Else
a(i, j) = a(i - 1, j - 1) + a(i - 1, j)
End If
str1 = str1 + Str(a(i, j)) + Space(4)
Next
str1 = str1 + Chr(13) + Chr(10)
Next
TextBox1.Text = str1 Dim a(3, 3), i, j As Integer
Dim str1, str2 As String
For i = 0 To 3
For j = 0 To 3
a(i, j) = Val(InputBox("请输入" & i & "行" & j & "列元素的值"))
If i = j And i + j = 3 Then
str1 = str1 + Str(a(i, j))
End If
Next
Next
MsgBox(str1) Dim s1, s2 As Integer
编写程序,建立并输出一个10*10的矩阵,
该矩阵主对角线和副对角线为1,其余元素为0,
结果显示在textbox1文本框中。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a(9, 9), i, j, n As Integer
Dim str1 As String = ""
For i = 0 To 9
For j = 0 To 9
a(i, j) = n
If i = j Or i + j = 9 Then
a(i, j) = 1
Else
a(i, j) = 0
End If
str1 = str1 + Str(a(i, j))
n = n + 1
Next
str1 = str1 + vbCrLf
Next
TextBox1.Text = str1
End Sub
展开阅读全文