当前位置:网站首页>题目:有一个已经按升序排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
题目:有一个已经按升序排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
2022-07-27 04:09:00 【弱冠初心】
<提示>程序分析:首先判断此数第一次小于数组中哪个元素,然后将此数插入,插入后此元素之后的数,依次后移一个
int[] nums = new int[5];
nums[0]=2;
nums[1]=5;
nums[2]=8;
nums[3]=9;
// 键盘输入一个数
Scanner iput = new Scanner(System.in);
System.out.println("请输入一个数:");
int insert = iput.nextInt();
int index = nums.length -1;
System.out.println("插入前的数组:");
for (int i : nums) {
System.out.print(i+" ");
}
System.out.println();
// 获取,替代下标
for(int i = 0;i<nums.length;i++){
if(insert<nums[i]){
index=i;
break;
}
}
// 其他向后挪
for(int i = nums.length-1;i>index;i--){
nums[i]=nums[i-1];
}
// 插入这个数据
nums[index]=insert;
System.out.println("插入后的数组:");
for (int i : nums) {
System.out.print(i+" ");
}
System.out.println("也该收一收这要了命的喜欢了");
边栏推荐
- e. The difference between target and e.currenttarget
- 安全第四次课后练习
- State Hook
- 干货 | 独立站运营怎么提高在线聊天客户服务?
- [untitled]
- 人很话不多,工程师不耍嘴皮子
- Prometheus node exporter common monitoring indicators
- 使用kubesphere图形界面dashboard开启devops功能
- Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
- Install and configure Debian on a wired network
猜你喜欢

结构型模式-适配器模式

第六章:云数据库

e.target与e.currentTarget的区别
![Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量](/img/ed/941276a15d1c4ab67d397fb3286022.png)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量

【C语言】递归详解汉诺塔问题

详解左值、右值、左值引用以及右值引用

网工知识角|只需四个步骤,教会你使用SecureCRT连接到eNSP,常用工具操作指南必看
![[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)](/img/6a/02c76f5bd35b2d89e4f471b9b688f5.png)
[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)

Easy to use shell shortcuts

Use the kubesphere graphical interface dashboard to enable the Devops function
随机推荐
Some common instructions in JVM tuning
项目参数做成可配置项,@ConfigurationProperties注解的使用
Head detached from origin/... Causes push failure
2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ ) type integer in
标准C语言13
Prometheus Node Exporter 常用监控指标
RN开发系列<9>--Mobx(1)入门篇
Spark practice case (upgraded version)
Deep analysis - dynamic memory management
JS three methods of traversing arrays: map, foreach, filter
influxDB 基础了解
sed输出指定行
The difference between ArrayList and LinkedList
The new Internet era has come. What new opportunities will Web 3.0 bring us
Ref Hook
Why does genericservlet have two init methods
你了解微信商户分账吗?
Hash (hash)
匿名命名管道, 共享内存的进程间通信理解与使用
Network knowledge corner | it only takes four steps to teach you to use SecureCRT to connect to ENSP. You must see the operation guide of common tools