当前位置:网站首页>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 ");
边栏推荐
- ELS compatibility DC, transfer pictures to window
- JS three methods of traversing arrays: map, foreach, filter
- Rust:axum learning notes (1) Hello World
- Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number
- Pinia入门到精通,Pinia使用全流程,包含state,actions,getters,以及如何解构,进行响应,actions使用的多种方法
- Post analysis of Data Analyst
- Okaleido ecological core equity Oka, all in fusion mining mode
- Brightcove appoints Dan Freund as chief revenue Officer
- [untitled]
- Ribbon load balancing principle and some source codes
猜你喜欢

使用Unity做一个艺术字系统

华为入局商用市场:趋势使然,挑战颇多

使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)

From scratch, C language intensive Lecture 4: array

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

HEAD detached from origin/...导致push失败

Dino paper accuracy, and analyze the variant of its model structure & Detr

e.target与e.currentTarget的区别

Echart柱状图中数据显示在图上方

The difference between ArrayList and LinkedList
随机推荐
F - Pre-order and In-order(Atcoder 255)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
Webpack packaging Vue project adds confusion to solve the cache problem
Some common instructions in JVM tuning
可视化领域 SVG
RSA asymmetric encryption and decryption signature verification tool
Oracle数据库字段date怎么才能走索引?
Word/excel has a fixed table size. When filling in the content, the table does not change with the cell content
e. The difference between target and e.currenttarget
人很话不多,工程师不耍嘴皮子
Understand kingbasees V9 in one picture
els_ 画矩形、代码规划和备份
卷积神经网络——24位彩色图像的卷积的详细介绍
法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
Okaleido生态核心权益OKA,尽在聚变Mining模式
从零开始C语言精讲篇4:数组
Brightcove任命Dan Freund为首席营收官
PX4模块设计之十二:High Resolution Timer设计
Install and configure Debian on a wired network