码迷,mamicode.com
首页 > 编程语言 > 详细

1146: 零起点学算法53——数组中插入一个数

时间:2017-04-08 22:21:41      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:else   mod   stat   起点   mst   ted   scanf   status   out   

1146: 零起点学算法53——数组中插入一个数

Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lld
Submitted: 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 }

 

1146: 零起点学算法53——数组中插入一个数

标签:else   mod   stat   起点   mst   ted   scanf   status   out   

原文地址:http://www.cnblogs.com/dddddd/p/6683190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!