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;}