Hdu 2019, hdu

Source: Internet
Author: User

Hdu 2019, hdu
Problem Description has n (n <= 100) integers, which have been arranged in ascending order. Now, to add an integer x, insert the number into the sequence, and make the new sequence still orderly. The Input data contains multiple test instances. Each group of data consists of two rows. The first row is n and m, and the second row is a series of ordered n numbers. If n and m are both 0, the end of the input data is indicated. We will not process the data. For each test instance, Output is the sequence after the new element is inserted. Sample Input3 3 1 2 4 0 0 Sample Output1 2 3 4 # include <stdio. h>
Int main ()
{
Int n, m, I, j, temp, cnt;
Int str [101];
While (scanf ("% d", & n, & m )! = EOF ){
If (n = 0 & m = 0) return 0;
Else {
For (I = 0; I <n; I ++ ){
Scanf ("% d", & str [I]);
}
If (str [n-1] <= m) str [n] = m;
Else {
For (I = 0; I <n-1; I ++ ){
If (str [I] <m & str [I + 1]> = m ){
Cnt = I;
For (j = n + 1; j> cnt + 1; j --){
Str [j] = str [J-1];
}
Str [cnt + 1] = m;
}
}
}
For (I = 0; I <n + 1; I ++ ){
Printf ("% d", str [I]);
If (I <n) printf ("");
Else printf ("\ n ");
}
}
}
Return 0;
} Tip: Specifies the size of the array. It also determines the insertion position. The value is smaller than or equal to the first part, and the value is greater than or equal to the later part.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.