【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 ...
随机推荐
- gridview-selector的设置
其实它是跟listview相似的,如果你看下它跟listview的继承关系,就很容易理解了 public class GridView extends AbsListView { /** * Disa ...
- NET Framework 4.5 有更加简便的方法 Task.Run()
NET Framework 4.5 有更加简便的方法 Task.Run()
- golang 建临时文件目录以及删除
package main import ( "fmt" "os" "path/filepath" "strings" ) ...
- arcgis webapp builder 安装试用
ArcGIS WebApp Builder 是针对开发者的,用于高速构建基于HTML5/Javascript 技术的美观的 Web应用的一个工具. 用过Flex版本号的AppBuilder应该非常清楚 ...
- hdu 2795 Billboard(线段树单点更新)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- dot 语法全介绍
0. 保存 保存为 pdf:dot -Tpdf iris.dot -o iris.pdf 1. 基本 (1)无向图.有向图.子图 graph G {} // 无向图 digraph G {} // 有 ...
- 26.SpringBoot事务注解详解
转自:https://www.cnblogs.com/kesimin/p/9546225.html @Transactional spring 事务注解 1.简单开启事务管理 @EnableTrans ...
- 第三次作业 201731082208 黄亚恒&肖莉
Github项目地址:https://github.com/HYHSTUDEY/WordCount.git 作业地址:https://www.cnblogs.com/hyhhyh090628/p/10 ...
- golang encoding/json
package main import ( "bytes" "encoding/json" "fmt" ) type ColorGroup ...
- 看<Asp.net夜话>随笔(2013-10-13)
1.Asp.net内置对象 1.1Request对象 封装了客户端请求信息 1.2Response对象 代表了服务器响应对象,可以向客户端返回数据 1.3Server对象 是用于获取服务器的相关信息的 ...