C语言入门题-数组中插入一个数

7-3 数组中插入一个数 (10分)

在一个已按升序排列的数组中插入一个数,插入后,数组元素仍按升序排列,已知升序数组{1, 2, 4, 6, 8, 9, 12, 15, 149, 156 }。

输入格式:

直接输入一个整数,没有其它任何附加字符。。

输出格式:

输出插入元素后的升序数列,每个数输出占5列。

输入样例:

5

输出样例:

     1    2    4    5    6    8    9   12   15  149  156
#include<stdio.h>
int main()
{
    
    
    int arr[10] = {
    
    1, 2, 4, 6, 8, 9, 12, 15, 149, 156},x,count=0;
    scanf("%d",&x);
    for(int i=0; i<10; i++)// 这题较为简单,找到相应位置插入即可
    {
    
    
        if(arr[0]>x&&count==0){
    
    
        	printf("%5d",x);
        	count++;
		}
		 printf("%5d",arr[i]);
        if(arr[i]<x&&arr[i+1]>x&&count==0){
    
    
        	printf("%5d",x);
        	count++;
		}
        if(arr[10]<x)printf("%5d",x);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_51198300/article/details/111596912