【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 ...
随机推荐
- 【2017 Multi-University Training Contest - Team 3】Kanade's sum
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6058 [Description] 给你n个数; 它们是由(1..n)组成的排列; 然后给你一个数字 ...
- 解决 Visual Studio 2013、2015、2017 工具箱不显示ArcGIS 10.2 控件,及ArcGIS模板丢失问题
1.重装ArcObject SDK for .NET Framework方法 (1)问题描述: 环境:WIN10 64bit.Visual Studio 2013.ArcGIS10.1.ArcGIS ...
- HBase高速导入数据--BulkLoad
Apache HBase是一个分布式的.面向列的开源数据库.它能够让我们随机的.实时的訪问大数据.可是如何有效的将数据导入到HBase呢?HBase有多种导入数据的方法.最直接的方法就是在MapRed ...
- Linux库文件路径的添加
库文件在连接(静态库和共享库)和运行(仅限于使用共享库的程序)时被使用,其搜索路径是在系统中进行设置的.一般 Linux 系统把 /lib 和 /usr/lib 两个目录作为默认的库搜索路径,所以使用 ...
- jQ-多选按钮实现单选按钮的功能以及input按钮的优化
css: .displayN{display: none;} label {font-size:12px;cursor:pointer;} label i {font-size:12px;font-s ...
- CISP/CISA 每日一题 20
CISSP 每日一题(答) What methods can be used to protectmobile devices such as a smartphone? Encryption,GPS ...
- C++ 复制到粘贴板
网上好多教程讲如何复制到剪切板,但是有可能复制的是乱码,为了方便,将CString类型的复制到剪切板 CString source;if (OpenClipboard()){//防止非ASCII语言复 ...
- arcgis webapp builder 安装试用
ArcGIS WebApp Builder 是针对开发者的,用于高速构建基于HTML5/Javascript 技术的美观的 Web应用的一个工具. 用过Flex版本号的AppBuilder应该非常清楚 ...
- hdu5301(2015多校2)--Buildings(构造)
Buildings Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
- libiconv 支持的编码
libiconv 支持的编码 php 中的 iconv() 函数常用来作编码转换用.作一些不同编码的动态数据的转换时常遇到一些未知编码的数据,这时 iconv() 支持那些编码转换就很重要. 刚开始, ...