ORIGIN

HDU-2019 Ordered Sequence

ACM 1 mins208 words

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

1
2
3
3 3
1 2 4
0 0

Sample Output

1
1 2 3 4

Analysis

Using C++ Sort.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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;
}
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|