Arithmetic

C语言-数组:在排好序的数组中插入一个数

PineappleCat · 4月24日 · 2019年 2297次已读

Description

按从小到大的顺序从键盘输入10个整数存入数组int a[11]中,再从键盘输入一个任意整数,将它插入到数组中,要求插入后数组仍然是从小到大排列的。
注意:不允许用排序的方法做 ,即不允许先把数存到数组中然后排序,而是应该先给这个数腾出位置,直接把它存到该位置
 

Input

输入10个数,然后再输入一个数
如:
1 3 5 7 9 11 13 15 17 19
12

Output

输出插入后的数组内容,如
1 3 5 7 9 11 12 13 15 17 19

Sample Input

1 3 5 7 9 11 13 15 17 19

12

Sample Output

1 3 5 7 9 11 12 13 15 17 19

Code 1 here:

#include <stdio.h>
int main()
{
	int a[11];
	int i,b,k=0;
	for(i=0;i<10;i++){
		scanf("%d",&a[i]);
	}
	scanf("%d",&b);
	for(i=0;i<10;i++){
		if(b<=a[i])
		{
			k=i;
			break;
		}
	}
	if(b>=a[9]){
		a[10]=b;
	}
	else{
		for(i=9;i>=k;i--){
		a[i+1]=a[i];
	}
	a[k]=b;
	}
	
	for(i=0;i<=9;i++){
		printf("%d ",a[i]);
	}
	printf("%d",a[10]);
    return 0;
}


Click here to view the copyright notice of this site(点击此处查看本站版权声明)
4 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!

  1. seek hope2021-5-6 · 11:24
    Microsoft Edge Microsoft Edge Windows Windows

    从键盘上输入任意10个数,将其按从小到大顺序排列

  2. 。。2020-10-28 · 14:59
    MIUI Browser MIUI Browser Android Android

    不大对啊