当前位置:网站首页>每日练习------有一组学员的成绩,将它们按降序排列,要增加一个学员的成绩,将它插入成绩序列,并保持降序
每日练习------有一组学员的成绩,将它们按降序排列,要增加一个学员的成绩,将它插入成绩序列,并保持降序
2022-07-26 19:59:00 【北柠陌语】
题目:有一组学员的成绩{99,82,85,60, 63},将它们按降序排列。要增加一个学员的成绩,将它插入成绩序列,并保持降序
解题关键: 降序排列,增加学员成绩后还是需要降序排列
思路: 1)先将原本数组按降序排列
2)新建一个比原来数组长度大1的数组
3)将原来数组里的元素按顺序存储带新的数组中
4)使用Scanner获取你要增加的学员成绩
5)通过循环比较,获取插入元素要插入的位置(下标)
6)从插入位置开始及之后的元素依往后移动一位(注意:移动的时候,从后向前移动)
7)移动元素之后,插入位置就空出来了,将插入元素存储到插入位置
过程: 接下来我们根据我们的解题思路来一步步写代码
//输出题目要求的数组
int[] scores = { 99, 82, 80, 60, 63 };
//1)先将原本数组按降序排列
for (int i = 0; i < scores.length; i++) {
for (int j = i + 1; j < scores.length; j++) {
if (scores[i] < scores[j]) {
int temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
break;
}
}
}
//遍历数组,看有没有降序成功
System.out.println("降序后的数组为:");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
}

//2)新建一个比原来数组长度大1的数组
int[] newScores = new int[scores.length + 1];
// 3)将原来数组里的元素按顺序存储带新的数组中
for (int i = 0; i < scores.length; i++) {
newScores[i] = scores[i];
}
System.out.println();
// 4)使用Scanner获取你要增加的学员成绩
Scanner sc = new Scanner(System.in);
System.out.println("请输入增加的学员成绩");
int insert = sc.nextInt();
// 5)通过循环比较,获取插入元素要插入的位置(下标)
int index = newScores.length - 1; //防止成绩为负分时出现错误
for (int i = 0; i < newScores.length; i++) {
if (insert > newScores[i]) {
index = i;
// 一旦获取插入元素比某一个元素大,就不再往后进行比较
break;
}
}
// 6)从插入位置开始及之后的元素依往后移动一位(注意:移动的时候,从后向前移动)
for (int i = newScores.length - 1; i > index; i--) {
newScores[i] = newScores[i - 1];
}
// 7)移动元素之后,插入位置就空出来了,将插入元素存储到插入位置
newScores[index] = insert;
// 插入元素之后,遍历新数组
System.out.println("插入成绩后的降序新数组为:");
for (int j = 0; j < newScores.length; j++) {
System.out.print(newScores[j] + " ");
}
完整结果如下:


总结:
这个程序主要用到的是数组循环的知识,其中有一部分程序是" int index = newScores.length - 1;"如果写成 "int index = 0",并且插入的成绩为负数,那么就会出现如下结果:

所以说这个的赋值需要大家额外注意.
为了方便大家使用,下面附上源码:
int[] scores = { 99, 85, 63, 60, 82 };
//1)先将原本数组按降序排列
for (int i = 0; i < scores.length; i++) {
for (int j = i + 1; j < scores.length; j++) {
if (scores[i] < scores[j]) {
int temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
break;
}
}
}
//输出降序完毕的数组
System.out.println("降序后的数组为:");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
}
// 2)新建一个比原来数组长度大1的数组
int[] newScores = new int[scores.length + 1];
// 3)将原来数组里的元素按顺序存储带新的数组中
for (int i = 0; i < scores.length; i++) {
newScores[i] = scores[i];
}
System.out.println();
// 4)使用Scanner获取你要增加的学员成绩
Scanner sc = new Scanner(System.in);
System.out.println("请输入增加的学员成绩");
int insert = sc.nextInt();
// 5)通过循环比较,获取插入元素要插入的位置(下标)
int index = 0;
for (int i = 0; i < newScores.length; i++) {
if (insert > newScores[i]) {
index = i;
// 一旦获取插入元素比某一个元素大,就不再往后进行比较
break;
}
}
// 6)从插入位置开始及之后的元素依往后移动一位(注意:移动的时候,从后向前移动)
for (int i = newScores.length - 1; i > index; i--) {
newScores[i] = newScores[i - 1];
}
// 7)移动元素之后,插入位置就空出来了,将插入元素存储到插入位置
newScores[index] = insert;
// 插入元素之后,遍历新数组
System.out.println("插入成绩后的降序新数组为:");
for (int j = 0; j < newScores.length; j++) {
System.out.print(newScores[j] + " ");
}明日练习:实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。
大家可以自己写写,明天中午12点我准时发出我的写法哦,明天12点不见不散
边栏推荐
猜你喜欢

Kotlin - coroutinecontext

Basic configuration and aggregation of BGP

BGP -- Border Gateway Protocol

Fitting the new direction of curriculum standards, ape guidance, creating a characteristic new concept content system

BUU刷题记-网鼎杯专栏2

Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions

Houdini 笔记2

Software testing - development test content specification (project test template)

LeetCode链表问题——19.删除链表的倒数第N个节点(一题一文学会链表)

Easycvr device management list page, paging data does not display problem repair
随机推荐
Sword finger offer46 translates numbers into strings
BUU刷题记1
LeetCode链表问题——19.删除链表的倒数第N个节点(一题一文学会链表)
BGP--边界网关协议
Quick start to connection pooling
The lawyer team of the US Department of justice asked the judge to refuse to accept Huawei's lawsuit
Gartner发布最新《中国AI初创企业市场指南》,弘玑Cyclone再次被评为代表性企业
Green and sustainable development of data center
数据块的存储系统中缓存的作用是什么?
Shell function, system function, basename [string / pathname] [suffix] can be understood as taking the file name in the path, dirname file absolute path, and user-defined function
Houdini finds the midpoint and connects the points to form a line
Leetcode刷题之——链表总结
When there are many query fields, you can add ordinary query and advanced query
[interview brush 101] dynamic planning 1
[基础服务] [数据库] ClickHouse的安装和配置
Opencv DNN deployment onnx model
从零开始搭建etcd分布式存储系统+Web管理界面
Experiment 6 BGP federal comprehensive experiment
Buu brush inscription 4
New features of ES6