【2013】将x插入有序数列
Time Limit: 3 second
Memory Limit: 2 MB
将一个数x插入到有序数列a中,插入后a仍然有序。
Input
第一行输入有序数列a的元素个数
第二行依次输入a的元素,以回车结束
第三行输入x的值
Output
输出插入x后的数列,每个元素用空格隔开,最后用回车结束
Sample Input
10
1 2 11 13 15 20 25 30 35 40
21
Sample Output
1 2 11 13 15 20 21 25 30 35 40
【题解】
这个插入要特别注意 小于最小值和大于最大值的情况。特判是比较明智的选择。。其他的只要for一遍
找到一个I 使得a[i] <= temp temp <= a[i+1]就好。然后从n+1开始for 到 i+2,a[j]=a[j-1];这样就能全部往后移一位。
【代码】
#include <cstdio>
#include <stdlib.h> const int MAXN = 30000; int n,temp,a[MAXN]; void output_ans() //把输出答案的步骤做成一个过程,方便特判。
{
for (int i = 1;i <= n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n+1]);
} void input_data()
{
scanf("%d",&n);
for (int i = 1;i <=n ;i++)
scanf("%d",&a[i]);
scanf("%d",&temp);
if (temp > a[n]) //特判两种情况,即小于最小值和大于最大值的情况
{
a[n+1] = temp;
output_ans();
exit(0);
}
if (temp < a[1])
{
for (int j = n+1;j >= 2;j--)
a[j] = a[j-1];
a[1] = temp;
output_ans();
exit(0);
}
} void get_ans()
{
int j;
for (int i = 1;i <= n-1;i++) //这是判断插入的位置。寻找到题解所需要的i
if (a[i] <= temp && temp <= a[i+1])
{
j = i;
break;
}
for (int k = n+1;k >= j + 2;k--) //这是将所有的数字往后移动一位的方法。
a[k] = a[k-1];
a[j+1] = temp;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
input_data();
get_ans();
output_ans();
return 0;
}
【2013】将x插入有序数列的更多相关文章
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- [LeetCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- office 2013幻灯片中插入SmartArt图形时出现错误下列一个或多个文件由于包含错误而无法运行
office 2013幻灯片中插入SmartArt图形时出现错误下列一个或多个文件由于包含错误而无法运行 系统:win8 64位 PowerPoint2013 64位 在幻灯片中插入SmartArt图 ...
- 有序数列第K小
有序数列第K小 题目描述 给出两个长度分别为\(n,m\)的单调非递减数列,求出它们合并后的第\(k\)小值. 输入输出格式 输入格式: 第一行三个数,\(n,m,k\)如题意所述: 第二行\(n\) ...
- 找出旋转有序数列的中间值python实现
题目给出一个有序数列随机旋转之后的数列,如原有序数列为:[0,1,2,4,5,6,7] ,旋转之后为[4,5,6,7,0,1,2].假定数列中无重复元素,且数列长度为奇数.求出旋转数列的中间值.如数列 ...
- [LeetCode] 21. Merge Two Sorted Lists 混合插入有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [LeetCode] 88. Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- LeetCode Summary Ranges (统计有序数列范围)
题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...
- 两个有序数列,求中间值 Median of Two Sorted Arrays
原题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
随机推荐
- highcharts 阶梯图表并填充颜色(自己觉得:直角折线图表)
例如以下:普通阶梯图 $(function () { $('#container').highcharts({ title: { text: '普通阶梯图' ...
- AIX中经常使用的SMIT 的使用
AIX中经常使用的SMIT 的使用 1. smit 的日志文件 (1)$HOME/smit.log 记录了所訪问的全部菜单.对话内容,所运行的命令和输出结果 在 SMIT 会话中出现的全部 ...
- Android Support 包里到底有什么
大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 随着 ...
- beego的orm ,用的数据库sqlite3
测试 beego的orm ,用的数据库sqlite3 1 package main import ( "fmt" "github.com/astaxie/beego/or ...
- redirect_uri 参数错误
http://www.cnblogs.com/zitjubiz/p/5935712.html http://blog.csdn.net/u014033756/article/details/52038 ...
- jsp+tomcat+ 创建project 配置project
*如今我们已经下载到了 tomcat 7.0+ eclipse for java ee 直接解压,打开eclipse. 接下来是步骤: eclipse 打开的界面.空空如也 ! ..* 点击 file ...
- Apache中PHP5.3 php5.4如何使用ZEND
Apache中PHP5.3 php5.4如何使用ZEND 有一套zend加密程序,需要安装ZEND,经过多次尝试,结果如下 由于PHP有安全线程(TS)和非安全线程(NTS)区分,PHP官方网站上说, ...
- hard-negative mining 及伪代码实现
Histogram of Oriented Gradients and Object Detection 获得 records 对于目标检测(object detection)问题,所谓的 hard- ...
- 文件上传流式处理commons-fileupload
1. 从请求中获取MultipartFile @RequestMapping(value="/upload", method=RequestMethod.POST) public ...
- Android LruCache类分析
public class LurCache<K, V> { private final LinkedHashMap<K, V> map; private int size; / ...