【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 ...
随机推荐
- 地道的 Python(二)
作者: Zhang Yang 列表推导 上文介绍了一个高逼格的创建字典的方法.那列表呢?依据蛋痛定律,它也一定有,可是它被起了一个很蛋痛的名字,叫列表推导: 先看看这种代码: li = [] for ...
- POJ 2828 线段树单点更新 离线搞
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- Harry Potter and the Goblet of Fire
书名:Harry Potter and the Goblet of Fire 作者:J.K. Rowling 篇幅: 752页 蓝思值:880L 用时: 17天 工具: 有道词典 [透析成果 ...
- struct数组初始化
const int MAXN=100; struct A { int a,b; }; struct A arr[100];//此时编译通过 struct A arr[MAXN];//此时编译不通过,原 ...
- iPad之Linux平台实践
updata.... 本文出自 "李晨光原创技术博客" 博客,谢绝转载!
- windows新建或者重命名文件及文件夹必须手动刷新才能显示出来
平台:win8.1 问题:windows新建或者重命名文件及文件夹必须手动刷新才能显示出来 解决方法: 注册表中HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ ...
- BZOJ3238: [Ahoi2013]差异(后缀数组)
Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Output 54 解题思路: 看到lcp,想到了 ...
- BZOJ 3732 Network Kruskal+倍增LCA
题目大意:给定一个n个点m条边的无向连通图.k次询问两点之间全部路径中最长边的最小值 NOIP2013 货车运输.差点儿就是原题...仅仅只是最小边最大改成了最大边最小.. . 首先看到最大值最小第一 ...
- CSU1660: K-Cycle
Description A simple cycle is a closed simple path, with no other repeated vertices or edges other t ...
- amazeui学习笔记一(开始使用1)--主干
amazeui学习笔记一(开始使用1)--主干 一.总结 1.英语:学好英语,编程轻松很多 2. layouts compatibility change log web app collection ...