当前位置:网站首页>Daily practice------There are 10 numbers that are required to be output from large to small by selection method
Daily practice------There are 10 numbers that are required to be output from large to small by selection method
2022-08-03 15:17:00 【North twist devoted to language】
Title: There are 10 numbers that are required to be output by selection method from large to small
The key to solving the problem:The interchange of numerical index
Ideas:1. Create an array
2. Input data
3. Find the largest index
4. Index interchange
Process: Next, we will write the code step by step according to our problem-solving ideas
1. Create an array
int[] nums = new int[10];
2. Input data
Scanner sc = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
System.out.print("The first" + (i + 1) + "The number of digits is:");
nums[i] = sc.nextInt();
}
3. Find the largest index
for (int i = 0; i < nums.length; i++) {
int index = 0;
for (int j = 0; j < nums.length - i; j++) {
if(nums[index] > nums[j]){
index = j;
}
}
">int temp = nums[nums.length - i - 1];
nums[nums.length - i - 1] = nums[index];
nums[index] = temp;
}
System.out.println("result:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
The full results are as follows:
For your convenience, the source code is attached below:
// 1. Create an arrayint[] nums = new int[10];// 2. Input dataScanner sc = new Scanner(System.in);for (int i = 0; i < nums.length; i++) {System.out.print("The first" + (i + 1) + "The number of digits is:");nums[i] = sc.nextInt();}// selection method// 3. Find the largest indexfor (int i = 0; i < nums.length; i++) {int index = 0;for (int j = 0; j < nums.length - i; j++) {if(nums[index] > nums[j]){index = j;}}//4. Index swapint temp = nums[nums.length - i - 1];nums[nums.length - i - 1] = nums[index];nums[index] = temp;}System.out.println("Result: ");for (int i = 0; i < nums.length; i++) {System.out.print(nums[i] + " ");}
Summary:After the index is swapped, the array is traversed, and then judged, you can use breakpoints to watch the operation of this code line by line
Exercise tomorrow:Find the sum of the diagonal elements of a 3*3 matrix
You can write by yourself, I will send my writing on time at 12 noon tomorrow, see you at 12 tomorrow
A lifetime of friends is a lifetime of love, and only with you will you win in your life; there is always love in a thousand mountains and rivers, just pay attention!
边栏推荐
- 接口测试主要测试什么?
- 一通骚操作,我把SQL执行效率提高了10000000倍!
- PAT乙级-B1013 数素数(20)
- 雷克萨斯lm的安全性如何,通过两个角度来聊这个话题
- Clickhouse Filling the Pit 3: Left Join changed to Right Join, resulting in incorrect statistical results
- 您的移动端app安全吗
- 8月份加密市场的三个关键预期 价格虽向北移动?预计仍将处于动荡之中
- WMS软件国内主要供应商分析
- 【FPGA教程案例44】图像案例4——基于FPGA的图像中值滤波verilog实现,通过MATLAB进行辅助验证
- NFT盲盒挖矿DAO智能合约dapp系统开发详情
猜你喜欢
Currency ATM: Solana Wallet Has Unknown Security Vulnerability, A Large Number Of Users' Digital Assets Are Stolen
cnpm 安装成功后提示不是内部和外部命令,也不是可运行的命令解决方案
技术分享 | 接口自动化测试如何搞定 json 响应断言?
网络中的交换机和路由器
16 【过渡 动画】
问题6:下拉框测试点
Ark server open tool, server tutorial win
测试基础整合-测试分类、软件质量模型、测试流程、测试用例、测试点划分方法、缺陷、例子
With a single operation, I improved the SQL execution efficiency by 10,000,000 times!
2021年12月电子学会图形化二级编程题解析含答案:消灭蝙蝠
随机推荐
实习路途:记录给我的第一个实习项目中的困惑
问题1:get和post的区别
PAT乙级-B1017 A除以B(20)
我现在推荐用mamba替代conda
HDU 1027 Ignatius and the Princess II(求由1-n组成按字典序排序的第m个序列)
接口测试主要测试什么?
【重构map】【重构filter】【重构Some】【重构reduce方法】【重构flat函数】
使用虚幻引擎自动化工具实现自动化部署
Linux安装Mysql的几种方法
一通骚操作,我把SQL执行效率提高了10000000倍!
PAT乙级-B1013 数素数(20)
liunx服务器nohup不输出日志文件的方法
夜神浏览器fiddler抓包
PAT乙级-B1010 一元多项式求导(25)
2021年12月电子学会图形化四级编程题解析含答案:聪明的小猫
php类的析构函数:__destruct
WMS软件国内主要供应商分析
PHP中高级面试题 – 第一天
2022-随便学学
js数组方法总结