Hangdian's 2019 series are ordered!

Source: Internet
Author: User
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2019 Sequence! Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 44844 accepted submission (s): 19451

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 Input
3 31 2 40 0
Sample output
1 2 3 4
Authorlcy seems to be a simple question, but it seems that it is not as simple as it is, it is not difficult, just be careful. *-* Two methods are provided for processing. AC code:
#include<iostream>#include<cstring>using namespace std;int a[101];int main(){int n,x,k,i;while(~scanf("%d%d",&n,&x)){memset(a,0,sizeof(a));if(n==0&&x==0)break;for(i=0;i<n;i++)scanf("%d",&a[i]);if(x>a[n-1])a[n]=x;else{/*i--;while(a[i]>x){a[i+1]=a[i];i--;}a[i+1]=x;}*/for(i=0;i<n;i++){if(x<a[i]){k=i;break;}}for(int j=n;j>k;j--){a[j]=a[j-1];}a[k]=x;}for(i=0;i<n;i++)printf("%d ",a[i]);printf("%d\n",a[n]);}return 0;}

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.