当前位置:网站首页>题目:有一个已经按升序排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
题目:有一个已经按升序排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
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("也该收一收这要了命的喜欢了");
边栏推荐
- Sed output specified line
- The data in echart histogram is displayed at the top of the chart
- pinia的持久化存储,pinia使用插件进行持久化存储。
- 可视化领域 SVG
- Word/excel has a fixed table size. When filling in the content, the table does not change with the cell content
- Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number
- 【day02】数据类型转换、运算符、方法入门
- 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
- Nacos startup and login
- Deep analysis - dynamic memory management
猜你喜欢

Spark practice case (upgraded version)

法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua

IIC 通信协议 (一)

从零开始C语言精讲篇4:数组

Brightcove任命Dan Freund为首席营收官

结构型模式-适配器模式

管理信息系统期末复习
![[day02] Introduction to data type conversion, operators and methods](/img/81/e2c49a4206e5d0d05308a1fc881626.png)
[day02] Introduction to data type conversion, operators and methods

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

卷积神经网络——24位彩色图像的卷积的详细介绍
随机推荐
Post analysis of Data Analyst
STM32 serial port based on Hal library accepts interrupts and idle interrupts
VSCode开启Pull Request更新代码分支可视化新篇章
无有线网络下安装并配置debian
可视化领域 SVG
There are two solutions for the feign call header of microservices to be discarded (with source code)
哈希表刷题(下)
sed输出指定行
使用kubesphere图形界面dashboard开启devops功能
Navicat exports Mysql to table structure and field description
Wechat applet rotation map
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in
使用Unity做一个艺术字系统
The difference between ArrayList and LinkedList
Ribbon load balancing principle and some source codes
timestamp列使用varchar类型和使用date类型有什么区别?
Okaleido tiger will log in to binance NFT in the second round, or continue to create sales achievements
Spark practice case (upgraded version)
Brightcove appoints Dan Freund as chief revenue Officer
标准C语言13