Randomize
Cls
Dim i As Long, N As Integer, S As String
For i = 1 To 100
A(i) = Int(Rnd * 90) + 10
S = S &A(i) &IIf(i Mod 10 = 0 And i <100, vbCrLf, " ")
Next
Print "随机生成的100个数为"
Print S
Print "其中的素数有:"
N = 0
T = 0
S = ""
For i = 1 To 100
If SuShu(A(i)) Then
N = N + 1
T = T + A(i)
S = S &A(i) &IIf(N Mod 10 = 0, vbCrLf, " ")
End If
Next
Print S
Print "素数的个数有:" &N &"个,其总和是:" &T
End Sub
请在窗体上添加三个文本框,将Multiline 属性设置为TruePrivate Sub Command1_Click()
Dim A(1 To 60) As Integer
Dim MinValue As Integer, MaxValue As Integer
MinValue = 100 '可能的最小值
MaxValue = 200 '可能的最大值
Randomize
For i = 1 To 60
A(i) = Int(Rnd * (MaxValue - MinValue + 1) + MinValue)
Next
S = ""
For i = 1 To 60
S = S &A(i) &IIf(i Mod 10 = 0, vbCrLf, " ")
Next
Text1.Text = S
'下面先对数组进行从小到大排序
For i = 1 To 59
For J = i To 60
If A(i) >A(J) Then
T = A(i)
A(i) = A(J)
A(J) = T
End If
Next
Next
J = 0
S = ""
For i = 1 To 60
If A(i) Mod 2 = 0 Then
J = J + 1
S = S &A(i) &IIf(J Mod 10 = 0, vbCrLf, " ")
End If
Next
Text2.Text = S
S = ""
J = 0
For i = 60 To 1 Step -1
If A(i) Mod 2 = 1 Then
J = J + 1
S = S &A(i) &IIf(J Mod 10 = 0, vbCrLf, " ")
End If
Next
Text3.Text = S
End Sub
程序如下:#include <stdio.h>
int a[4][4]={{1,2,3,4},{4,5,6,1},{7,8,9,1},{10,11,12,1}}
int ahang[4],alie[4]
void main()
{
int sum=0
for(int i=0i<4i++)
{
for(int j=0j<4j++)
{
sum+=a[i][j]
ahang[i]+=a[i][j]
alie[j]+=a[i][j]
}
}
printf("%d\n",sum)
int k,o
int min,max
int temp,index
for(k=0k<3k++)
{
min=ahang[k]
index=k
for(o=k+1o<4o++)
{
if(ahang[o]<min)
{
min=ahang[o]index=o
}
}
temp=ahang[k],ahang[k]=min,ahang[index]=temp
}
for(k=0k<4k++)
{
printf("%3d",ahang[k])
}
printf("\n")
for(k=0k<3k++)
{
max=alie[k]
index=k
for(o=k+1o<4o++)
{
if(ahang[o]>max)
{
min=alie[o]index=o
}
}
temp=alie[k],alie[k]=min,alie[index]=temp
}
for(k=0k<4k++)
{
printf("%3d",alie[k])
}
printf("\n")
}
说明:
用两个for循环,计算数组元素总和sum,和各行元素子和ahang[i]和各列之和alie[i],0=<i<=3
下面用选择排序法,分别对ahang[i]和alie[i]排序.只是一个选择最小,一个最大,算法几乎一致.
程序是可以使用的.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)