Sequence!
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 46658 accepted submission (s): 20141
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 31 2 40 0
Sample output1 2 3 4
1 #include<cstdio> 2 int main() 3 { 4 int n,m,a[100]={0}; 5 while(scanf("%d%d",&n,&m)!=EOF) 6 { 7 if(n==0&&m==0)return 0; 8 for(int i=0;i<n;i++)scanf("%d",&a[i]); 9 a[n]=m;10 for(int i=0;i<n;i++)11 for(int j=i+1;j<=n;j++)12 if(a[i]>a[j]){m=a[i];a[i]=a[j];a[j]=m;}13 14 for(int i=0;i<n;i++)printf("%d ",a[i]);15 printf("%d\n",a[n]);16 }17 }
Hangzhou (Hangzhou), 2019 20:49:34