1、VB常见常用的编程题代码常见常用的程序代码输入n个整数求最大值 Sub Main() Console.WriteLine(输入n个整数) Dim s As String = Console.ReadLine() Dim A() As String = s.Split( ) Dim aa(A.Length - 1) As Integer For i As Integer = 0 To A.Length - 1 aa(i) = Convert.ToInt32(A(i) Next Dim max As Integer = aa(0) For i As Integer = 1 To aa.Length
2、 - 1 If aa(i) max Then max = aa(i) End If Next Console.WriteLine(最大数为0, max) End Sub 输出n之间的素数 Sub main() Dim n As Integer = Convert.ToInt32(Console.ReadLine() If n 2 Then Console.WriteLine(输入错误) End If For i As Integer = 2 To n If isprime(i) Then Console.Write(i) Console.Write( ) End If Next Console
3、.WriteLine() End Sub Function isprime(ByVal n As Integer) As Boolean If n = 2 Then Return True Else Dim i As Integer = 2 While i = n / i If n Mod i = 0 Then Return False End If i = i + 1 End While Return True End If End Function 3.字符串也有求长度和按 (i) 输出的功能 Sub Main() Dim expression As String Console.Writ
4、eLine(输入一个字符串) expression = Console.ReadLine For i As Integer = 0 To expression.Length - 1 Console.Write(expression(i) Console.Write( ) Next End Sub 4.输入年月日输出这是该年中的第几天 闰年的条件:能被4整除但不能被100整除或能被400整除Sub main() Dim a() As Integer = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 Dim b() As Integer = 31,
5、29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 Console.WriteLine(输入年月日) Dim m As String = Console.ReadLine() Dim msplit() As String = m.Split(,) Dim n, month, data As Integer Dim sum As Integer = 0 n = Convert.ToInt32(msplit(0) month = Convert.ToInt32(msplit(1) data = Convert.ToInt32(msplit(2) If n = 1
6、Then sum = data End If If (n Mod 4 = 0) And (n Mod 100 0) Or (n Mod 400 = 0) Then For i As Integer = 0 To month - 2 sum = sum + b(i) Next Else : For i As Integer = 0 To month - 2 sum = sum + a(i) Next End If sum = sum + data Console.Write(sum) End Sub 线性表和栈.队列题型1. 电话号码本Structure people Dim name As S
7、tring Dim number As Integer End Structure Structure listtype Dim data() As people Dim n As Integer End StructureSub Main() Dim list As listtype Dim max As Integer = 1000 ReDim list.data(max) Dim p1, p2, p3 As people p1.name = haha p1.number = 23456Sub insert(ByRef l As listtype, ByVal i As Integer,
8、ByVal x As people) If i = 0 And i i l.data(j) = l.data(j - 1) j = j - 1 End While l.data(i) = x l.n = l.n + 1 Else : Console.WriteLine(位置不正确) End IfEnd Sub Sub delete(ByRef l As listtype, ByVal i As Integer) If i = 0 And i l.n Then While i l.n l.data(i) = l.data(i + 1) i = i + 1 End While l.n = l.n
9、- 1 Else : Console.WriteLine(位置不正确) End If End Sub Sub print(ByRef l As listtype) For i As Integer = 0 To l.n - 1 Console.WriteLine(l.data(i).name) Console.WriteLine(l.data(i).number) Next End Sub 2.检查括号是否匹配 a.用new stcak 实现 Sub Main() Dim expression As String Console.WriteLine(输入一个字符串) expression =
10、Console.ReadLine() Dim s As New Stack(Of String) s.Clear() Dim ch As String For i As Integer = 0 To expression.Length - 1 ch = expression(i) If ch = ( Then s.Push(ch) End If If ch = ) Then If s.Count 0 Then Console.WriteLine(不匹配) Else: s.Pop() End If End If End if If s.Count = 0 Then Console.WriteLi
11、ne(匹配) Else Console.WriteLine(不匹配) End If End Sub B.用数组实现 Structure stack Dim ch() As String Dim top As Integer Dim bottom As Integer End Structure Sub Main() Dim examp As stack Dim max As Integer = 10000 ReDim examp.ch(max) Console.WriteLine(输入字符串) Dim expression As String = Console.ReadLine() For
12、i As Integer = 0 To expression.Length - 1 If expression(i) = ( Then push(examp, expression(i) End If If expression(i) = ) Then If examp.top s.bottom Then s.top = s.top - 1 Return s.ch(s.top) Else : Return vbNull End IfEnd Function 3.将一组字符串进行反序 Sub Main() Console.WriteLine(输入字符串) Dim expression As St
13、ring = Console.ReadLine Dim exam As New Stack(Of String) exam.Clear() Dim ch As String For i As Integer = 0 To expression.Length - 1 ch = expression(i) exam.Push(ch) Next While exam.Count 0 Console.Write(exam.Pop() End While End Sub 判断是否为回文序列Sub Main() Console.WriteLine(输入字符串) Dim expression As Stri
14、ng = Console.ReadLine() Dim exam1 As New Stack(Of String) Dim exam2 As New Stack(Of String) exam1.Clear() exam2.Clear() Dim ch As String For i As Integer = 0 To expression.Length - 1 ch = expression(i) exam1.Push(ch) ch = expression(expression.Length - 1 - i) exam2.Push(ch) Next Dim flag As Boolean
15、= True For i As Integer = 0 To expression.Length - 1 If exam1.Pop() exam2.Pop() Then flag = False Exit For End If Next If flag = True Then Console.WriteLine(y) Else : Console.WriteLine(n) End If End Sub 5. 折半查找Sub Main() Dim a() As Integer = 1, 2, 3, 4, 5, 6, 7, 8 Dim k = binsearch(a, 4) If k = 0 Th
16、en Console.WriteLine( 查找的数据在数组中的位置为: + k.ToString() Else Console.WriteLine( 数据没有找到) End If End Sub Function binsearch(ByVal a() As Integer, ByVal key As Integer) As Integer Dim low As Integer = 0 Dim high As Integer = a.Length - 1 While low = high Dim mid As Integer = (low + high) / 2 If key = a(mid
17、) Then Return mid If key = 0 Then Console.WriteLine(查找的位置在0, k) Else : Console.WriteLine(不存在) End If End Sub Function search(ByVal s() As Integer, ByVal key As Integer) As Integer For i As Integer = 0 To s.Length - 1 If s(i) = key Then Return i End If Next Return -1End Function 7.冒泡排序 Sub Main() Dim
18、 s() As Integer = 1, 2, 3, 4, 5, 6, 7 show(s) bubble(s) show(s) End Sub Sub show(ByVal s() As Integer) For Each k As Integer In s Console.Write(k.ToString() + ) Next End Sub Sub bubble(ByVal s() As Integer) Dim lenghth As Integer = s.Length For i As Integer = 0 To lenghth - 1 For j As Integer = leng
19、hth - 1 To i + 1 Step -1 If s(j) s(j - 1) Then Dim temp As Integer = s(j) s(j) = s(j - 1) s(j - 1) = temp End If Next Next End Sub 8.选择排序 Sub Main() Dim s() As Integer = 2, 9, 7, 5 show(s) Console.WriteLine(排序结果是) Selectsort(s) show(s) End Sub Sub show(ByVal s() As Integer) For Each k As Integer In
20、s Console.Write(k.ToString() + ) Next End Sub Sub selectsort(ByVal s() As Integer) Dim lenghth As Integer = s.Length For i As Integer = 0 To lenghth - 2 For j As Integer = i + 1 To lenghth - 1 If s(j) s(i) Then Dim temp As Integer = s(j) s(j) = s(i) s(i) = temp End If冒泡排序Dim lenghth As Integer = s.L
21、engthFor i As Integer = 0 To lenghth - 1 For j As Integer = lenghth - 1 To i + 1 Step -1 If s(j) s(j - 1) Then Dim temp As Integer = s(j) s(j) = s(j - 1) s(j - 1) = temp End If Next Next快速排序Dim lenghth As Integer = s.Length For i As Integer = 0 To lenghth - 2 For j As Integer = i + 1 To lenghth - 1 If s(j) s(i) Then Dim temp As Integer = s(j) s(j) = s(i) s(i) = temp End If Next
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1