HDU-2019 Ordered Sequence

See the article on https://dyingdown.github.io/2019/12/20/HDU-2019%20Ordered-Sequence/

Ordered Sequence

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

Input

The input data contains multiple test cases. Each set of data consists of two rows, the first row is n and m, and the second row is a sequence of n numbers that have been ordered. n and m are 0 at the same time to mark the end of the input data, this line does not do processing.

Output

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

Sample Input

3 3
1 2 4
0 0

Sample Output

1 2 3 4

Analysis

Using C++ Sort.

Code

#include<bits/stdc++.h>

using namespace std;

int arr[1000];
int main() {
	int n, m;
	while(cin >> n >> m, n, m){
		for(int i = 0; i < n; i ++) {
			cin >> arr[i];
		}
		arr[n] = m;
		sort(arr, arr + n + 1);
		for(int i = 0; i <= n; i ++) {
			if(i == n) cout << arr[i] << endl;
			else cout << arr[i] << " ";
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值