当前位置:网站首页>Title: there is an array that has been sorted in ascending order. Now enter a number and ask to insert it into the array according to the original rule.
Title: there is an array that has been sorted in ascending order. Now enter a number and ask to insert it into the array according to the original rule.
2022-07-27 04:35:00 【Weak crown initial heart】
< Tips > Program analysis : First, determine which element in the array this number is smaller than for the first time , Then insert this number , The number after this element is inserted , Move one back in turn
int[] nums = new int[5];
nums[0]=2;
nums[1]=5;
nums[2]=8;
nums[3]=9;
// Enter a number on the keyboard
Scanner iput = new Scanner(System.in);
System.out.println(" Please enter a number :");
int insert = iput.nextInt();
int index = nums.length -1;
System.out.println(" Array before insertion :");
for (int i : nums) {
System.out.print(i+" ");
}
System.out.println();
// obtain , Substitute subscript
for(int i = 0;i<nums.length;i++){
if(insert<nums[i]){
index=i;
break;
}
}
// Others move backwards
for(int i = nums.length-1;i>index;i--){
nums[i]=nums[i-1];
}
// Insert this data
nums[index]=insert;
System.out.println(" Array after insertion :");
for (int i : nums) {
System.out.print(i+" ");
}
System.out.println(" It's time to accept it. I like it desperately ");
边栏推荐
- [day02] Introduction to data type conversion, operators and methods
- Rust:axum learning notes (1) Hello World
- 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
- 第六章:云数据库
- 深度学习领域图像分割FCN(Fully Convolutional Networks for Semantic Segmentation)
- Brightcove appoints Dan Freund as chief revenue Officer
- The new Internet era has come. What new opportunities will Web 3.0 bring us
- Convolution neural network -- convolution of gray image
- Do you know about wechat merchant billing?
- [small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation
猜你喜欢

Use the kubesphere graphical interface dashboard to enable the Devops function

Prometheus node exporter common monitoring indicators

Easy to use shell shortcuts

Okaleido ecological core equity Oka, all in fusion mining mode

深度剖析 —— 动态内存管理

使用kubesphere图形界面dashboard开启devops功能

Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)

2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in

VSCode开启Pull Request更新代码分支可视化新篇章

【独立站建设】跨境电商出海开网店,首选这个网站建设!
随机推荐
[C language] recursively explain the tower of Hanoi problem
ELS square display principle
sram、dram、sdram、ddr的区别和用途
使用kubesphere图形界面dashboard开启devops功能
Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing
P1438 无聊的数列 线段树+差分
【HCIP】重发布、重分布、重分发实验
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
els 兼容性DC、传递图片到窗口
PX4模块设计之十二:High Resolution Timer设计
Redis interview question (2022)
Px4 module design 12: high resolution timer design
你了解微信商户分账吗?
Spark practice case (upgraded version)
使用Unity做一个艺术字系统
Ribbon load balancing principle and some source codes
管理信息系统期末复习
干货 | 独立站运营怎么提高在线聊天客户服务?
[competition reference] pytorch common code snippet and operation collection
BigDecimal pit summary & Best Practices