在一个已按升序排列的数组中插入一个数,插入后,数

2024-12-23 08:33:05
推荐回答(4个)
回答1:

#include
#include
#define N 11
main()
{
int i,j,t,number,a[N]={1,2,4,6,8,9,12,15,149,156};
a[10]=0;
printf("please enter an integer to insert in the array:\n");
scanf("%d",&number);
printf("The original array:\n");
for(i=0;i printf("%5d",a[i]);
printf("\n");
for (i=N-2;i>=0;i--)
{
if(number>a[0])
{
if (number>a[i])
{
for (j=N-1;j>i+1;j--)
{
a[j]=a[j-1];
}
a[i+1]=number;
break;
}
}
else
{
for(j=N-1;j>0;j--)
a[j]=a[j-1];
a[0]=number;
break;
}
}
printf("The result array:\n");
for(i=0;i printf("%5d",a[i]);
printf("\n");
}

我看楼上 在输入数字大于 原数组最大数跟输入数字小于原数组最小数的时候,会出错,改了下。

回答2:

你问题没说清楚,插入一个数后然后要怎么样???说清楚了我帮你改!

补充:

#include
#include
#define N 11
main()
{
int i,j,t,number,a[N]={1,2,4,6,8,9,12,15,149,156};
printf("please enter an integer to insert in the array:\n");
scanf("%d",&number);
printf("The original array:\n");
for(i=0;i printf("%5d",a[i]);
printf("\n");
for (i = 0; i < N-1; i++)
{
if (number >= a[i] && number <= a[i+1])
{
for (j=N-2; j >= i+1; j--)
a[j+1] = a[j];
a[i+1]=number;
break;
}
}
printf("The result array:\n");
for(i=0;i printf("%5d",a[i]);
printf("\n");
}

回答3:

#include
#define
N
11
void
main()
{
int
i,j,number,a[N]={1,2,4,6,8,9,12,15,149,156};
printf("please
enter
an
integer
to
insert
in
the
array:\n");
/**********FOUND**********/
scanf("%d",&number)
;
printf("The
original
array:\n");
for(i=0;iprintf("%5d",a[i]);
printf("\n");
/**********FOUND**********/
i=0;
while
(i=a[i])
i++;
for(j=N-1;j>=i;j--)
/**********FOUND**********/
a[j]=a[j-1];
/**********FOUND**********/
a[i]=number;
printf("The
result
array:\n");
for(i=0;iprintf("%5d",a[i]);
printf("\n");
}

回答4:

#include
#define N 11
void main()
{
int i,j,number,a[N]={1,2,4,6,8,9,12,15,149,156};
printf("please enter an integer to insert in the array:\n");
/**********FOUND**********/
scanf("%d",&number) ;
printf("The original array:\n");
for(i=0;i printf("%5d",a[i]);
printf("\n");
/**********FOUND**********/
i=0;
while (i=a[i]) i++;
for(j=N-1;j>=i;j--)
/**********FOUND**********/
a[j]=a[j-1];
/**********FOUND**********/
a[i]=number;
printf("The result array:\n");
for(i=0;i printf("%5d",a[i]);
printf("\n");
}