c语言把一个数组插入一个数组中,插入后数组仍然有序
程序员文章站
2024-03-15 20:24:30
...
#include <stdio.h>
#define M 50
void fun(int a[],int x,int *n)
{
int i;
a[*n]=x;
i=*n-1;
while (a[i]>x&&i>=0) {
a[i+1]=a[i];
i--;
a[i+1]=x;
*n+=1;
}
}
int main(int argc, const char * argv[]) {
int a[M],x,n,k;
//clrscr();
printf("\nplease enter n:");
scanf("%d",&n);
printf("\nenter %d number(from small to large):\n",n);
for(k=0;k<n;k++)
scanf("%d",a+k);
printf("\nenter x:\n");
scanf("%d",&x);
printf("\n");
fun(a,x,&n);
printf("\nthe result is:(from small to large):\n");
for(k=0;k<n;k++)
printf("%3d",a[k]);
printf("\n");
return 0;
}
output:
please enter n:10
enter 10 number(from small to large):
2 4 6 8 10 12 14 16 18 20
enter x:
11
the result is:(from small to large):
2 4 6 8 10 11 12 14 16 18 20
下一篇: js获取最大值
推荐阅读