当前位置:网站首页>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!
边栏推荐
猜你喜欢
2021年12月电子学会图形化四级编程题解析含答案:森林运动会
币圈提款机:Solana钱包出现未知安全漏洞 大量用户数字资产被盗
雷克萨斯lm的安全性如何,通过两个角度来聊这个话题
R7 6800H+RTX3050+120Hz 2.8K OLED screen, Intrepid Pro15 2022 pre-sale
MMA安装及使用优化
上亿数据怎么玩深度分页?兼容MySQL + ES + MongoDB
6000 字+,帮你搞懂互联网架构演变历程!
With a single operation, I improved the SQL execution efficiency by 10,000,000 times!
指令重排以及案例
2021年12月电子学会图形化三级编程题解析含答案:数星星
随机推荐
高等数学(第七版)同济大学 习题4-1 个人解答
2021年12月电子学会图形化三级编程题解析含答案:跳高比赛
How to play deep paging with hundreds of millions of data?Compatible with MySQL + ES + MongoDB
问题9:为什么有边界值的测试?
文件包含之伪协议的使用
PAT乙级-B1018 锤子剪刀布(20)
如何用二分法搜索、查找旋转数组中是否含有某个(目标)值? leetcode 81.搜索旋转排序数组
devops-2:Jenkins的使用及Pipeline语法讲解
一文搞懂$_POST和php://input的区别
mysql占用服务器CPU100%的解决办法
With a single operation, I improved the SQL execution efficiency by 10,000,000 times!
今日睡眠质量记录75分
程序员面试必备PHP基础面试题 – 第二十天
【FPGA教程案例44】图像案例4——基于FPGA的图像中值滤波verilog实现,通过MATLAB进行辅助验证
使用Typora+EasyBlogImageForTypora写博客,无图床快速上传图片
[The Beauty of Software Engineering - Column Notes] 36 | What exactly do DevOps engineers do?
【周报】2022年7月24日
8月份加密市场的三个关键预期 价格虽向北移动?预计仍将处于动荡之中
网络中的交换机和路由器
无内鬼,来点干货!SQL优化和诊断