VB求解题~谢谢

2024-12-22 01:57:17
推荐回答(4个)
回答1:

1.求二维数组中最大

Dim a(2, 3) As Integer
Dim max As Integer, row As Integer, column As Integer

a(1, 1) = 34: a(1, 2) = 34: a(1, 3) = 43
a(2, 1) = 34: a(2, 2) = 78: a(2, 3) = 12
Dim i, j, m, n As Integer

For i = 1 To 3

For i = 1 To 3
m = n = 0
If a(m)(n) < a(i)(j) Then
m = i
n = j

End If
max = a(m)(n)
row = m
column = n

Next

Next

2.(事件)双击窗体

Private Sub Form_DbClick()
Dim a, s As Long
For i = 1 To 10
a = 1
For j = 1 To i
a = a * j
Next
s = s + a
Next
MsgBox s
End Sub

3.编写函数fun其功能
Function fun(w)
Dim n
n = Log(w) / Log(10) + 1
If n Mod 5=0 Then
fun = 1
Else
fun = k
End If
End Function

4.事件)单击窗体
Private Sub Form_Click()

Dim M&, N&

For x = N To M
y = 0
For i = 1 To (x - 1)
If x Mod i = 0 Then y = y + i
Next i
If y = x Then Print x; "是一个完全数。"
Next x
end sub

5.求两数m,n的最小公倍数

t1=m 't1,t2是临时变量
t2=n
g = IIf(m < n, m, n)
Do While g > 1
If m Mod g = 0 And n Mod g = 0 Then Exit Do
g = g - 1
Loop
Print m & "与" & n & "的最小公倍数为:" & (t1*t2)/g

6.(事件)单击窗体
Private Sub Form_Click()
dim sum as integer

sum = 0
s = 100
y = 500
Do
s = s + 1
If s Mod 2 Then
sum = sum + 1
End If

Loop Until s = y
Print sum
end sub

7.对字符串进行加密处理
加密的思想是:

将每个字母C加(或减)一序数K,即用它后的第K个字母代替,变换式公式: c=chr(Asc(c)+k)

例如序数k为5,这时 "A"→ "F", "a"→ "f","B"→ "G"…

当加序数后的字母超过"Z"或"z"则 c=Chr(Asc(c)+k -26)

例如:You are good→ Dtz fwj ltti

解密为加密的逆过程

将每个字母C减(或加)一序数K,即 c=chr(Asc(c)-k),

例如序数k为5,这时 "Z"→"U", "z"→"u","Y"→"T"…

当加序数后的字母小于"A"或"a"则 c=Chr(Asc(c)-k +26)

下段程序是加密处理:

i = 1: strp = ""

nL = Len(RTrim(strI))

Do While (i <= nL)

strT = Mid$(strI, i, 1) '取第i个字符

If (strT >= "A" And strT <= "Z") Then

iA = Asc(strT) + 5

If iA > Asc("Z") Then iA = iA - 26

strp = strp + Chr$(iA)

ElseIf (strT >= "a" And strT <= "z") Then

iA = Asc(strT) + 5

If iA > Asc("z") Then iA = iA - 26

strp = strp + Chr$(iA)

Else

strp = strp + strT

End If

i = i + 1

Loop

Print strp

8.单击窗体
Private Sub Form_Click()
Dim I As Integer
Dim S1, S2, S3 As Integer
Form1.Cls

For I = 1 To 100
If I Mod 5 = 0 Then
Print I;
S1 = S1 + I
End If
Next I
Print Chr(13) & "1-100之间被5整除的数之和为:S1=" & S1
For I = 1 To 100
If I Mod 7 = 0 Then
Print I;
S2 = S2 + I
End If
Next I
Print Chr(13) & "1-100之间被7整除的数之和为:S2=" & S2
For I = 1 To 100
If (I Mod 5 = 0) And (I Mod 7 = 0) Then
Print I;
S3 = S3 + I
End If
Next I
Print Chr(13) & "1-100之间被7和5整除的数之和为:S3=" & S3
Print Chr(13) & "1-100之间被7或5整除的数之和为:S4=" & S1 + S2 - S3
End Sub

回答2:

1.设三个变量:max,row,col;二重循环判断,分别存最大值,最值的行和列;
2.数据会溢出,在VB里解决这个问题比较复杂,自带的数据类型得不到精确值,可以考虑用数组实现,每个元素只存结果中的一位,注意元素1存个位,类推.
上面的第二个问题转化为:1!,2!,3!....10!的和,即ary(n)=1/*最初始值,也就是1!*/,sum(n)+=ary(n);下一步:把ary(n)各元素乘2得到2!,再做一次sum(n)+=ary(n);继续下一步:把ary(n)各元素乘3得到3!,再做一次sum(n)+=ary(n);直接到ary(n)乘10.注意:A.进位调整,也就是乘完后判断每一位,超过10就只保留个位,把10位以上的数字加到下一位数上(也就是人为的进行进位调整);B.最高位进位引起数组长度变化要用REDIM语句改变数组长度;C.sum(n)在加ary(n)之前要保证长度大于等于ary(n)的长度,这样不会丢数;D.元素1存个位,元素2存十位...结果就要反过来写.
3.设整数为N
dim sum as integer
while N>0
dim tmp as integer
tmp = n mod 10
sum = sum + tmp * tmp
sum = sum / 10
N = N / 10
wend
if(sum mod 5=0) then
return 1
else
return 0
原理:能不能被5整除只要看个位,公当个位是5或0时能被5整除,所以只要留下个位就可以了.
4.以后的问题跟1一样没什么含量,自己去做吧,永远不动手就永远不会做.

回答3:

2. dim a,s as integer for i=1 to 10 a=1 for j=1 to i a=a*j next j s=s+a next i

回答4:

4bv