1092: Exercise 6-4 Ordered Insertion

Topic description

There is a sorted array, after a number is required to be input, it is inserted into the array according to the original sorting rule.

Assuming that the length of the array is 10, the first 9 numbers in the array (these 9 numbers need to be entered from the keyboard, and the input order must be in the order from small to large) have been sorted from small to large.

Then input an integer from the keyboard, and insert this integer into the previous 9 numbers, so that the final 10 numbers are still ordered from small to large.

enter

The first line is to enter 9 integer numbers separated by spaces, which are required to be entered in ascending order.

Enter an integer on the second line

output

Output these 10 numbers from small to large, one line for each number.

sample input

1 11 21 31 41 51 61 71 81
45

Sample output

1
11
21
31
41
45
51
61
71
81

hint

When defining an array, define the length of the array as 10. C Language Fourth Edition Teaching Experiment - Chapter 6 Arrays

#include<stdio.h>
int main(){
	int a[10],m,i,j,t1,t2;
	for(i=0;i<9;i++)
	scanf("%d",&a[i]);
	scanf("%d",&m);
	if(m>a[8])
	a[9]=m;
	else{
		for(i=0;i<10;i++){
		if(a[i]>m){
			t1=a[i];
			a[i]=m;
		    for(j=i+1;j<10;j++){
		     	t2=a[j];
		    	a[j]=t1;
			    t1=t2;
	     	}
		break;
    	}
	    }
	}
	for(i=0;i<10;i++)
		printf("%d\n",a[i]);
		return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846679&siteId=291194637