1146: 零起點學算法53——數組中插入一個數
Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 1749 Accepted: 613
[Submit][Status][Web Board]
Description
給定有序數組(從小到大),再給你一個數,要求插入該數到數組中並保持順序
Input
多組測試,每組第一行輸入一個整數n,然后是n個有序的整數
第二行輸入1個整數m和1個整數K
Output
將整數m插入到原數組中保持順序是升序,然后輸出2行
第一行是插入以后的數組
第二行是插入以后的數組中下標值是K的數
n m k不超過20
Sample Input 
3 1 2 5
3 1
Sample Output
1 2 3 5
2
Source
1 #include<stdio.h> 2 int main(){ 3 int n,a[50],b[50]; 4 while(scanf("%d",&n)!=EOF){ 5 for(int i=0;i<n;i++){ 6 scanf("%d",&a[i]); 7 } 8 9 int m,k,j=0; 10 scanf("%d%d",&m,&k); 11 for(int i=0;i<n;i++){ 12 if(a[i]<m){ 13 b[j]=a[i]; 14 j++; 15 } 16 else break; 17 } 18 b[j]=m; 19 j++; 20 for(int i=j-1;i<n;i++){ 21 b[j]=a[i]; 22 j++; 23 } 24 25 for(int i=0;i<j-1;i++){ 26 printf("%d ",b[i]); 27 } 28 printf("%d\n",b[j-1]); 29 printf("%d\n",b[k]); 30 } 31 return 0; 32 }
