VB算法特训营(二)

这真的很重要!关于题目版权声明及部分事项 ? 戳它进行查看

刷题刷题,(部分算法题可以用Python写,毕竟算法都是互通的…)

img

51.计时器应用

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Private Sub Command1_Click()
Static flag As Integer
flag = flag + 1
If flag Mod 2 = 1 Then
Timer1.Enabled = True
Command1.Caption = "停止"
Else
Timer1.Enabled = False
Command1.Caption = "开始计时"
MsgBox "一共运行了" & Val(Label1) \ 3600 & "小时" & Val(Label1) \ 60 Mod 60 & "分" & Val(Label1) Mod 60 & "秒"
End If
End Sub

Private Sub Timer1_Timer()
Static i As Integer
i = i + 1
Label1 = i
End Sub

[collapse title=”降智写法”]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Public flag As Integer
Dim s As Integer
Dim m As Integer
Dim h As Integer
Private Sub Command1_Click()
Cls
flag = flag + 1
If flag Mod 2 = 1 Then
Timer1.Enabled = True
s = 0
m = 0
h = 0
Command1.Caption = "停止"
Else
MsgBox "一共运行了" & h & "小时" & m & "分" & s & "秒", , "工程1"
Timer1.Enabled = False
Command1.Caption = "计时开始"
End If
End Sub
Private Sub Timer1_Timer()
Cls
s = s + 1
If s >= 60 Then
s = 0
m = m + 1
ElseIf m >= 60 Then
h = h + 1
End If
Print h, m, s
End Sub

[/collapse]

52.点餐系统

img

代码

1
2
3
4
5
6
7
8
9
10
11
Dim a, b, c As Integer
If Check1 = 1 Then
s = s + Val(Text1) * 18
End If
If Check2 = 1 Then
s = s + Val(Text2) * 23
End If
If Check3 = 1 Then
s = s + Val(Text3) * 28
End If
MsgBox "一共" & s & "元"

53.霓虹灯

img

代码

这题有两种做法(?)

第一种

1
2
3
4
5
Static i As Integer
res = "VB编程俱乐部"
i = i + 1
Label5(i) = Mid(res, i, 1)
If i = 7 Then Timer4.Enabled = False

第二种

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Public n As Integer
Dim a(1 To 7) As String
Private Sub Form_Click()
s = "VB编程俱乐部"
For i = 1 To 7
res = Mid(s, i, 1)
a(i) = res
Next
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
n = n + 1
Label1 = Label1 & Mid(a(n), 1, n) & Space(1)
If n = 7 Then
Timer1.Enabled = False
End If
End Sub

54.拨号

img

代码

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Dim t As String, i As Integer
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
Text1 = Text1 & Command1(0).Caption
Case 1
Text1 = Text1 & Command1(1).Caption
Case 2
Text1 = Text1 & Command1(2).Caption
Case 3
Text1 = Text1 & Command1(3).Caption
Case 4
Text1 = Text1 & Command1(4).Caption
Case 5
Text1 = Text1 & Command1(5).Caption
Case 6
Text1 = Text1 & Command1(6).Caption
Case 7
Text1 = Text1 & Command1(7).Caption
Case 8
Text1 = Text1 & Command1(8).Caption
Case 9
Text1 = Text1 & Command1(9).Caption
End Select
End Sub
Private Sub Command11_Click()
t = Text1
i = 0
Text1 = ""
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Text1 = ""
i = i + 1
Text1 = Text1 & Mid(t, 1, i)
If i = Len(t) Then
Timer1.Enabled = False
End If
End Sub

55.排序字符串

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Dim num(), str()
For i = 1 To Len(Text18)
res = Mid(Text18, i, 1)
If res >= 0 And res <= 9 Then
n = n + 1
ReDim Preserve num(n)
num(n) = num(n) + res
Else
s = s + 1
ReDim Preserve str(s)
str(s) = str(s) + Asc(res)
End If
Next
For i = 1 To UBound(num)
For j = 1 To UBound(num)
If num(i) < num(j) Then t = num(i): num(i) = num(j): num(j) = t
Next
Next
For i = 1 To UBound(str)
For j = 1 To UBound(str)
If str(i) < str(j) Then t = str(i): str(i) = str(j): str(j) = t
Next
Next
For i = 1 To UBound(num): Print num(i);: Next
For i = 1 To UBound(str): Print Chr(str(i));: Next

[collapse title=”旧的代码”]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Public n
Private Sub Command1_Click()
Cls
Dim num() As Integer
Dim caption()
s = LCase(Text1)
For i = 1 To Len(s)
res = Mid(s, i, 1)
If IsNumeric(res) Then
n = n & res
p = p + 1
Else
r = r & res
l = l + 1
End If
Next
ReDim Preserve num(p)
ReDim Preserve caption(l)
For i = 1 To UBound(num)
num(i) = Val(Mid(n, i, 1))
For j = 1 To UBound(num)
If num(i) < num(j) Then
t = num(i)
num(i) = num(j)
num(j) = t
End If
Next
Next
For i = 1 To UBound(caption)
caption(i) = Asc(Mid(r, i, 1))
For j = 1 To UBound(caption)
If caption(i) < caption(j) Then
t = caption(i)
caption(i) = caption(j)
caption(j) = t
End If
Next
Next
For i = 1 To UBound(num)
k = k & num(i)
Next
Print k;
For o = 1 To UBound(caption)
Print Chr(Val(caption(o)));
Next
End Sub

[/collapse]

56.排序题

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Private Sub Form_Click()
Cls
Dim a(1 To 10) As Integer
Print "排序前数据:"
For i = 1 To 10
a(i) = Int(Rnd * (99 - 10 + 1)) + 10
Print a(i);
Next
Print
Print "排序后数据:"
For i = 1 To 10
For j = 1 To 10 - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next
Next
For i = 1 To 10
Print a(i);
Next
End Sub

57.统计数字

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Public zero, one, n, l, s
Private Sub Command1_Click()
one = "1"
s = s & one
l = l + 1
End Sub
Private Sub Command2_Click()
zero = "0"
s = s & zero
n = n + 1
End Sub
Private Sub Command4_Click()
Label1 = "这个二进制数是:" & s
Label2 = "这个数中零的个数是:" & n & ",1的个数是:" & l
End Sub
Private Sub Command5_Click()
one = ""
zero = ""
n = 0
l = 0
End Sub

58.计时器

img

代码

1
2
3
4
5
6
7
8
9
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Label1.Left = Label1.Left + 100
End Sub

59.倒计时

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Private Sub Command46_Click()
Timer6.Enabled = True
End Sub
Private Sub Option1_Click(Index As Integer)
Select Case Index
Case 0
n = 60
Case 1
n = 300
Case 2
n = 600
End Select
End Sub
Private Sub Timer6_Timer()
n = n - 1
Label7 = (n \ 60) Mod 60 & "分" & n Mod 60 & "秒"
If n = 0 Then
MsgBox "时间到!"
Timer6.Enabled = False
End If
End Sub

[collapse title=”错误代码”]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Dim s As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
Frame1.Enabled = False
Label1.Enabled = False
Label1.Caption = (s \ 60) Mod 60 & "分" & s Mod 60 & "秒"
End Sub
Private Sub Option1_Click()
s = 60
End Sub
Private Sub Option2_Click()
s = 300
End Sub
Private Sub Option3_Click()
s = 600
End Sub
Private Sub Timer1_Timer()
If s = 0 Then
Label1.Caption = "时间到!"
Frame1.Enabled = True
Label1.Enabled = True
Timer1.Enabled = True
Else
s = s - 1
Label1.Caption = (s \ 60) Mod 60 & "分" & s Mod 60 & "秒"
End If
End Sub

[/collapse]

60.判断质数

img

代码

1
2
3
4
5
6
7
8
9
10
11
Private Sub Text1_Change()
Cls
For i = 2 To Val(Text1) - 1
If Val(Text1) Mod i = 0 Then Exit For
Next
If i > Val(Text1) - 1 Then
Print "是质数"
Else
Print "不是质数"
End If
End Sub

61.求降序数

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Private Sub Form_Click()
Cls
Print "所有2位的降序数有:"
For i = 10 To 99
a = i \ 10 Mod 10 '十位
b = i Mod 10 '个位
If a >= b Then
Print i;
n = n + 1
End If
If n = 10 Then
Print
n = 0
End If
Next
End Sub

62.判断三角形

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a = Val(Text1)
b = Val(Text2)
c = Val(Text3)
If a + b > c And a + c > b And b + c > a Then
If a = b And b = c Then
Print "等边三角zhi形daozhuan"
ElseIf a = b Or b = c Or a = c Then
Print "等腰三角形"
Else
Print "三角形"
End If
Else
Print "不能构成三角形"
End If

63.求守形数

img

代码

1
2
3
4
5
6
7
Cls
Print "1000以内的守形数有:"
For i = 1 To 1000
If i ^ 2 Mod 10 = i Or i ^ 2 Mod 100 = i Or i ^ 2 Mod 1000 = i Then
Print i
End If
Next

64.文字移动

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Dim flag As Boolean
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
Public Sub Timer1_Timer()
If Label1.Top <= 0 Or Label1.Top >= Form1.Height - 650 _
Or Label1.Left <= 0 Or Label1.Left >= Form1.Width - 850 Then
If Check1.Value = 1 Then
Timer1.Enabled = False ' 判断是否到达边界
End If
End If
If Option1 = True Then
lr
End If
If Option2 = True Then
td
End If
End Sub
Private Sub lr()
If flag = False Then
Label1.Left = Label1.Left + 10
If Label1.Left >= Form1.Width - 850 Then
flag = True
End If
Else
Label1.Left = Label1.Left - 10
If Label1.Left = 0 Then
flag = False
End If
End If
End Sub
Private Sub td()
If flag = False Then
Label1.Top = Label1.Top + 10
If Label1.Top > Form1.Height - 650 Then
flag = True
End If
Else
Label1.Top = Label1.Top - 10
If Label1.Top = 0 Then
flag = False
End If
End If
End Sub

65.偶数和和奇数平方和

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
Cls
Dim a(10), i, sum As Integer
For i = 1 To 10
a(i) = Int(Rnd * (300 - 100 + 1)) + 100
Print a(i);
If a(i) Mod 2 = 0 Then
sum = sum + a(i)
Else
sum1 = sum1 + a(i) ^ 2
End If
Next
Print
Print "偶数和和奇数平方和:" & sum & " " & sum1

66.打印题

img

代码

1
2
3
4
5
6
7
8
9
10
11
Cls
n = Val(Text1)
If n >= 1 And n <= 9 Then
For i = 1 To n
Print Space(n - i);
For j = 1 To i
Print "*";
Next
Print
Next
End If

67.求水仙花

img

代码

1
2
3
4
5
6
7
8
9
Print "水仙花数有:"
For i = 100 To 999
a = i Mod 10
b = i \ 10 Mod 10
c = i \ 100
If (a ^ 3 + b ^ 3 + c ^ 3) = i Then
Print i
End If
Next

68.偶数比奇数多

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
Dim a(10) As Integer
For i = 1 To 10
a(i) = Int(Rnd * (1000 - 100 + 1)) + 100
Print a(i);
If a(i) Mod 2 <> 0 Then
j = j + 1
Else
o = o + 1
End If
Next
Print
Print "偶数比奇数多: " & o - j & " 个"

69.统计个数

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
Text1 = ""
Dim a(10) As Integer
For i = 1 To 10
a(i) = Int(Rnd * (99 - 10)) + 10
Text1 = Text1 & a(i) & Space(1)
If a(i) Mod 2 = 0 Then
o = o + 1
Else
j = j + 1
End If
Next
Label1 = "奇数的个数是:" & j & "偶数的个数是:" & o

70.a到b中的和

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
Public a, b As Integer
Private Sub Command1_Click()
a = Val(InputBox("请输入A"))
End Sub
Private Sub Command2_Click()
b = Val(InputBox("请输入B"))
End Sub
Private Sub Command3_Click()
For i = a To b
s = s + i
Next
Label1 = "a-b之间的和是:" & s
End Sub

71.String函数应用

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
Private Sub Command1_Click()
Label1 = ""
If Option1 = True Then
Label1 = Label1 & String(Val(Text1), "好")
End If
If Option2 = True Then
Label1 = Chr(Val(Text1))
End If
End Sub
Private Sub Form_Load()
Text1 = Val(Int(Rnd * (90 - 65 + 1)) + 65)
End Sub

72.显示A-D之间的字母

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Public a, b As String
Private Sub Command1_Click()
a = InputBox("请输入字母a")
End Sub
Private Sub Command2_Click()
b = InputBox("请输入字母b")
End Sub
Private Sub Command3_Click()
Label1 = ""
For i = Asc(a) To Asc(b)
s = s & Chr(i) & Space(1)
Next
For i = Asc(b) To Asc(a)
s = s & Chr(i) & Space(1)
Next
Label1 = a & "到" & b & "之间的子母有:" & s
End Sub

73.个位数是3的个数

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
Cls
Dim a(10) As Integer
For i = 1 To 10
a(i) = Int(Rnd * (99 - 11 + 1)) + 11
Print a(i);
b = Right(a(i), 1)
If b = 3 Then
n = n + 1
End If
Next
Print
Print "个位数是3的个数是:" & n

74.打印题

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
n = Val(text1)
If n >= 1 And n <= 20 Then
For i = 1 To n
For j = 1 To n
If i >= j Then
Print "1 ";
Else
Print "0 ";
End If
Next
Print
Next
End If

75.统计字数

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
Label1 = ""
Label2 = ""
s = Text1
For i = 1 To Len(s)
res = Mid(s, i, 1)
t = t & res & Space(1)
If res Like "[a-z]" Then
n = n + 1
End If
Next
Label1 = "间隔输出:" & t
Label2 = "小写字母个数是:" & n

76.选课

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Private Sub Command1_Click()
For i = 0 To List1.ListCount - 1
If List1.List(List1.ListIndex) = List2.List(i) Then
MsgBox "已选过这门课!", , "工程1"
Exit Sub
End If
Next
List2.AddItem List1.List(List1.ListIndex)
End Sub
Private Sub Form_Load()
List1.AddItem "Java"
List1.AddItem "VB 6.0"
List1.AddItem "C#"
List1.AddItem "网页制作"
List1.AddItem "软件测试"
List1.AddItem "ASP.net"
List1.AddItem "PHP"
List1.AddItem "HTML5"
End Sub

77.算法题

img

代码

78.打印矩阵

img

代码

1
2
3
4
5
6
7
8
9
10
For i = 1 To 5
For j = 1 To 5
If 6 - i = j Then
Print "0 ";
Else
Print "1 ";
End If
Next
Print
Next

79.验证密码

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Public s As String
Private Sub Command1_Click()
s = s & "a"
End Sub
Private Sub Command2_Click()
s = s & "b"
End Sub
Private Sub Command3_Click()
s = ""
End Sub
Private Sub Command4_Click()
If s = "abab" Then
Form2.Show
Me.Hide
End If
End Sub

80.求玫瑰花

img

代码

1
2
3
4
5
6
7
8
9
For i = 1000 To 9999
a = i Mod 10
b = i \ 10 Mod 10
c = i \ 100 Mod 10
d = i \ 1000
If (a ^ 4 + b ^ 4 + c ^ 4 + d ^ 4) = i Then
Print i
End If
Next

81.列表应用

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
Private Sub Command1_Click()
Text1 = ""
Text1 = "您选中的是第 " & List1.ListIndex + 1 & " 项 菜单名为:" & List1
End Sub
Private Sub Form_Load()
List1.AddItem "宫保鸡丁"
List1.AddItem "水煮活鱼"
List1.AddItem "鱼香茄子"
List1.AddItem "佛跳墙"
List1.AddItem "椒盐虾"
List1.AddItem "香瓜蒸鹌鹑蛋"
End Sub

82.打印矩阵

img

代码

1
2
3
4
5
6
7
8
9
10
For i = 1 To 5
For j = 1 To 5
If i = j Then
Print "0 ";
Else
Print "1 ";
End If
Next
Print
Next

83.求零巧数img

代码

1
2
3
4
5
6
7
8
9
For i = 1000 To 9999
a = Mid(i, 1, 1)
b = Mid(i, 3, 1)
c = Mid(i, 2, 1)
d = Mid(i, 4, 1)
If (a + b + d) * 9 = i And c = 0 Then
Print i
End If
Next

84.整除个数

img

代码

1
2
3
4
5
6
7
8
9
10
Dim a(10) As Integer
For i = 1 To 10
a(i) = Int(Rnd * (600 - 300 + 1)) + 300
If a(i) Mod 13 = 0 Then
n = n + 1
End If
Print a(i);
Next
Print
Print "能被13整除的个数是:" & n

85.m到n之间奇数和

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
m = Val(Text1)
n = Val(Text2)
If m > n Then
t = m
m = n
n = t
End If
For i = m To n
If i Mod 2 <> 0 Then
s = s + i
End If
Next
Print s

86.调价方案

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
n = Val(Text1)
If n > 3 Then
a = 10 + (n - 3) * 2
b = 8 + (n - 3) * 2.5
Else
a = 10
b = 8
End If
If a = b Then
MsgBox "一样贵", , "VB3"
ElseIf a > b Then
MsgBox "方案一贵", , "VB3"
Else
MsgBox "方案二贵", , "VB3"
End If

87.算法题

img

代码

88.数组最大最小值

随机生成10个数字(10-99)并放入到一维数组中,找出他们的最大最小值

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Dim a(10) As Integer
For i = 1 To 10
a(i) = Int(Rnd * (99 - 10)) + 10
Print a(i);
Next
Print
Max = a(1): Min = a(1)
For i = 1 To 10
If a(i) > Max Then
Max = a(i)
End If
If a(i) < Min Then
Min = a(i)
End If
Next
Print "最大值是:" & Max & " 最小值是:" & Min

89.计算整数和

img

代码

1
2
3
4
5
6
For i = 0 To 200
If i Mod 3 = 0 Or i Mod 7 = 0 Then
s = s + i
End If
Next
Print s

90.统计字符串

img

代码

1
2
3
4
5
6
7
8
res = Text2
For i = 1 To Len(Text1)
resp = Mid(Text1, i, 1)
If res = resp Then
n = n + 1
End If
Next
Label1 = n

91.字符处理

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Private Sub Check1_Click()
If Check1 = 1 Then
Text1.FontUnderline = True
Else
Text1.FontUnderline = False
End If
End Sub
Private Sub Check2_Click()
If Check2 = 1 Then
Text1.FontItalic = True
Else
Text1.FontItalic = False
End If
End Sub
Private Sub Option1_Click()
Text1.Font = "宋体"
End Sub
Private Sub Option2_Click()
Text1.Font = "隶书"
End Sub

92.字符串处理

img

代码

1
2
3
4
5
6
7
8
9
10
Cls
For i = 1 To Len(Text1)
res = Mid(Text1, i, 1)
If res Like "[A-Z]" Then
s = s & LCase(res)
Else
s = s & UCase(res)
End If
Next
Text1 = s

93.调价方案

img

代码

94.打印题

img

代码

1
2
3
4
5
6
7
8
n = 9
For i = 1 To n
Print Space(i);
For j = i To n * 2 - i
Print "*";
Next
Print
Next

95.字符加密

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Private Sub Command1_Click()
Cls
Text2 = ""
For i = 1 To Len(Text1)
res = Asc(Mid(Text1, i, 1))
If res = 57 Then
res = 64
End If
res = res + 1
s = s & Chr(res)
Next
Text2 = s
End Sub
Private Sub Command2_Click()
Text3 = ""
Text3 = StrReverse(Text2)
End Sub

96.勾股数

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
For a = 1 To 50
For b = a To 50
For c = b To 50
If a ^ 2 + b ^ 2 = c ^ 2 Then
n = n + 1
End If
Next c, b, a
Print n
For a = 1 To 50
For b = 1 To 50
c = Sqr(a ^ 2 + b ^ 2)
If c = Int(c) Then
If c <= 50 Then
n = n + 1
End If
End If
Next b, a
Print n \ 2

97.打印题

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
n = InputBox("请输入图形的行数(1-9)", "输入")
If n >= 1 And n <= 9 Then
For i = 1 To 9
Print Space(9 - i);
For j = 1 To i * 2 - 1
If j Mod 2 = 0 Then
Print "*";
Else
Print Trim(i);
End If
Next
Print
Next
End If

98.打印成绩

img

补充:分别求出(最高分,平均分,优秀率>=90为优秀)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Cls
Randomize
Dim s As Integer
Print "姓名" & Space(2) & "科目1" & _
Space(1) & "科目2" & _
Space(1) & "科目3" & _
Space(1) & "科目4" & _
Space(1) & "最高分" & _
Space(1) & "平均分" & _
Space(1) & "优秀率"
Dim a(4, 8)
Max = 0
For i = 1 To 4
For j = 1 To 8
If j = 1 Then
a(i, 1) = "学生" & i
End If
If j > 1 And j < 6 Then
a(i, j) = Space(2) & Int(Rnd * (99 - 50 + 1)) + 50 & Space(2)
s = s + a(i, j)
End If
If a(i, j) >= 90 And j <> 1 Then
n = n + 1
End If
If a(i, j) > Max And j <> 1 And j <> 6 And j < 7 Then
Max = a(i, j)
End If
a(i, 6) = Max & Space(1)
a(i, 7) = Val(Round(s / 4, 0)) & Space(1)
a(i, 8) = FormatPercent(n / 4, 0, vbTrue)
Print a(i, j);
Next
s = 0
Max = 0
n = 0
Print
Next

99.项数

img

代码

1
2
3
4
5
6
7
8
Dim a, n As Integer, s As Double
a = -1
n = Val(InputBox("请输入项数", "项数"))
For i = 1 To n
a = -a
s = s + a * (i / (i + 1))
Next
MsgBox "前" & n & "项的和是:" & s, vbInformation, "求和"

[collapse title=”旧的代码”]

1
2
3
4
5
6
7
8
9
10
11
Dim s As Double
n = Val(InputBox("请输入项数", "项数"))
s = 0
For i = 1 To n
If i Mod 2 = 0 Then
s = s + i / -(i + 1)
Else
s = s + i / (i + 1)
End If
Next
MsgBox "前" & n & "项的和是:" & s, vbInformation

[/collapse]

100.非素数之和

img

代码

1
2
3
4
5
6
7
8
For i = 10 To 15
s = 1
For j = 1 To i - 1
If i Mod j = 0 Then s = s + 1
Next
If s <> 2 Then Sum = Sum + i
Next
Print Sum
Author

IceCliffs

Posted on

2021-02-09

Updated on

2024-12-03

Licensed under

Comments