已知一个数组a大小为100个元素,用随机函数生成20个不同的2位整数,放在该数组的0~19下标段(后面的元素设为0),再将这20个数组元素从小到大排序并输出。再任意输入一个2位整数,要求将该数插入数组中合适的位置,使得这个数组中的21个元素仍然按照从小到大的顺序排列,输出这21个元素。
#include <stdio.h>
void put(int t[],int n)
{
int j=0;
while(j<n)
{
printf("%d ",t[j]);
j++;
}
printf("\n");
}
int main()
{
int a[100];
int j,k;
for(j=0;j<20;j++)
{
a[j]=rand()%100;
}
int n2=20;
while(n2-1)
{
j=0;
while(j<n2)
{
if(a[j]>a[j+1])
{
int tem=a[j];
a[j]=a[j+1];
a[j+1]=tem;
}
j++;
}
n2--;
}
put(a,20);
int b;
scanf("%d",&b);
int pos=0;
for(j=0;j<20;j++)
{
if(a[j]<=b)
{
pos=j+1;
}
}
//printf("pos:%d\n",pos);
for(j=21;j>pos;j--)
{
a[j]=a[j-1];
}
a[pos]=b;
put(a,21);
return 0;
}