发布网友 发布时间:2022-05-07 06:14
共2个回答
热心网友 时间:2023-10-18 18:15
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
#define F 1 //1 显示数列,0不显示
int main( )
{
int i,a[N],num,flag;
srand(time(NULL));
while(1)
{
flag=0;
printf("开始生成%d个2位随机整数数组成的数列\n",N);
for(i=0;i<N;i++)
a[i]=rand()%100;
if(F)
for(i=0;i<N;i++)
printf("%d ",a[i]);
printf("\n请输入一个数:");
scanf("%d",&num);
for(i=0;i<N;i++)
if(a[i]==num)
{
flag=1;
printf("%d在数列第%d个元素的位置\n",num,i+1);
break;
}
if(!flag)
printf("%d不在数列中\n",num);
}
return 1;
}
热心网友 时间:2023-10-18 18:15
Dim a() As IntegerPrivate Sub Command1_Click() Cls Randomize (Timer) Dim n As Integer n = InputBox("请输入所生成的数组中包含的元素个数", , 10) ReDim a(n - 1) For i = LBound(a) To UBound(a) a(i) = Int(Rnd * 900 + 100) Next i For i = LBound(a) To UBound(a) - 1 For j = i + 1 To UBound(a) If a(j) < a(i) Then t = a(i) a(i) = a(j) a(j) = t End If Next j Next i For i = LBound(a) To UBound(a) Print a(i); If (i + 1) Mod 10 = 0 Then Print Next i End Sub Private Sub Command2_Click() Dim n As Single, Flag As Boolean n = InputBox("请输入要插入的数n:", , 500) ReDim Preserve a(UBound(a) + 1) Flag = True For i = LBound(a) To UBound(a) If n < a(i) Then For j = UBound(a) To i + 1 Step -1 a(j) = a(j - 1) Next j a(i) = n Flag = False Exit For End If Next i If Flag Then a(UBound(a)) = n Print Print "插入" & n & ":" For i = LBound(a) To UBound(a) Print a(i); If (i + 1) Mod 10 = 0 Then Print Next i PrintEnd Sub Private Sub Form_Load() Me.AutoRedraw = TrueEnd Sub