有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

public static void main(String[] args) {
int[] a = {1,3,5,7,9,19,29,70};
int[] b = new int[a.length+1];
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个数:");
int insert = scan.nextInt();
int n = 0;
//当插入的数小于第一个数时
if(insert < a[0]){
b[0] = insert;
for(int i = 1 ;i < b.length;i++){
b[i] = a[i-1];
}
}else if(insert > a[a.length-1]){//当插入的数大于最后一个数时

for(int i = 0 ;i < b.length-1;i++){
b[i] = a[i];
}
b[b.length-1] = insert;
}else {
//当插入的数在中间时
int j = 0;
for( ; j < a.length-1 ; j++){

if(insert > a[j] && insert < a[j+1]){
n = j;
}
}
for(int k = 0;k <= n ;k++){
b[k]= a[k];
}
b[n+1] = insert;
for(int k = n+2; k <b.length;k++){
b[k] = a[k-1];
}
}

for(int c = 0 ; c <b.length;c++){
System.out.print(b[c]+" ");
}

}

}

时间: 2024-08-26 22:37:52

有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。的相关文章

58.有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中

#include<iostream> using namespace std; int main() { int n,k; int temp; int a[11]={1,4,6,9,11,13,35,44,46,50}; cout<<"原数组是:"<<endl; for(int p=0;p<10;p++) { cout<<a[p]<<" "; } cout<<endl; cout<&

有一个从小到大排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> //有一个从小到大排好序的数组.现输入一个数,要求按原来的规律将它插入数组中. var arr = [1, 2, 3, 4, 5, 7, 9]; function insertScot

c++数组。输入10个数,输出这10个数中的最大值,最小值。

1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 int a[10]; 6 int min=0, max=0; 7 cout << "Please enter 10 integer:" << endl; 8 for(int i=0; i<10; i++) 9 { 10 cin >> a[i]; 11 } 12 min = a[0]; 13 max = a[0

[经典面试题]输入一个排好序的数组的一个旋转,输出旋转数组的最小元素。

[题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1. [分析] 这道题最直观的解法并不难.从头到尾遍历数组一次,就能找出最小的元素,时间复杂度显然是O(N).但这个思路没有利用输入数组的特性,我们应该能找到更好的解法. 我们注意到旋转之后的数组实际上可以划分为两个排序的子数组,而且前面的子数组的元素都大于或者等于后面

将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。

int a[3] = {12, 15, 17}; int b[4] = { 2, 8, 16, 22}; int c[7] = {0}; int i = 0, j = 0, k = 0; while (i < 3 && j < 4 ) { if (a[i] > b[j]) { c[k++] = b[j++]; } else { c[k++] = a[i++]; } } while (i < 3) { c[k++] = a[i++]; } while (j <

[算法]对于已排好序的数组进行去重

最近项目中遇到一个场景,需要对排好序的数组进行去重(场景就是对同时获取两个频道的消息列表时做一个合并,而消息列表里的数据已经是排序好的), 经过思考后,尝试写了一个,感觉还算简洁,这里分享一下,如有有缘人可以参考,遇到问题还望指正,欢迎讨论:) 1 #include <iostream> 2 3 using namespace std; 4 5 #define NUM 7 6 7 int main(){ 8 9 int a[NUM] = {1, 1, 2, 2, 3, 5, 5}; 10 11

合并两个已经排好序的不等长的数组

给两个已经排好序的数组,这两个数组的长度可能不相等,如何将他们合并? package airth; public class TestMergeArray { /**  * 功能:  * 作者: jiangfuqiang  * 创建日期:2014-10-13  * 修改者: mender  * 修改日期: modifydate  * @param args  */ public static void main(String[] args) { // TODO Auto-generated me

判断一个数组是否是一个二叉排序树的后序遍历结果

比如给出数组[5,7,6,9,11,10,8]判断其是否为二叉排序树的后序遍历结果,也就是能不能画出一个二叉排序树使其的后序遍历结果与这个数组相同,若可以返回true,不可以返回false. 代码: int is_valid(int *data, int n){ if(data==NULL)return 0; int left=1; int right=1; int i=0; int j; int root=data[n-1]; while(data[i]<root){ i++; } for(j

【算法导论学习-016】两个已排过序的等长数组的中位数(median of two sorted arrays)

问题来源 <算法导论>P223 9.3-8: Let X[1..n] and Y[1..n] be two arrays, each containing nnumbers already in sorted order. Give an O(lgn)-time algorithm to find themedian of all 2n elements in arrays X and Y. 翻译过来即:求两个等长(n个元素)的已排序数组A和B的中位数 方案1:对两个数组进行归并直到统计到第n