The numbers are in order!

The numbers are in order!

Time Limit: 1000 ms  Memory Limit: 65536 KiB

Problem Description

There are n (n<=100) integers, which have been arranged in ascending order. Now give another integer m, please insert this number into the sequence and make the new sequence still in order.

Input

The input data contains multiple test instances, each set of data consists of two rows, the first row is n and m, and the second row is an ordered sequence of n numbers. Both n and m are 0, indicating the end of the input data, and this line does not process it.

Output

For each test instance, output the sequence after inserting the new element.

Sample Input

3 3
1 2 4
0 0

Sample Output

1 2 3 4

Hint

 

Source

HDOJ

Note: To be considered, if the inserted number is in the last digit, special discussion is required


#include<stdio.h>
int main(){
	int n,m;
	int i,j;
	int a[101];
	while(scanf("%d %d",&n,&m)!=EOF&&(m!=0&&n!=0)){
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(i=0;i<n;i++){
		if(m<=a[i]&&m>=a[i-1]){
			for(j=n-1;j>=i;j--){
				a[j+1]=a[j];
			}
			a[i]=m;
			break;
		}
		if(a[n-1]<m){
			a[n]=m;
		}

	}
	for(i=0;i<n+1;i++){
		if(i==0){
			printf("%d",a[i]);
		}else{
			printf(" %d",a[i]);
		}
	}
	printf("\n");
	}
}

Guess you like

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