当前位置:网站首页>每日练习------有一组学员的成绩,将它们按降序排列,要增加一个学员的成绩,将它插入成绩序列,并保持降序
每日练习------有一组学员的成绩,将它们按降序排列,要增加一个学员的成绩,将它插入成绩序列,并保持降序
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点不见不散
边栏推荐
- 884. 两句话中的不常见单词-哈希表
- YGG cooperates with my pet hooligan, AMGI's flagship NFT project, to enter the rabbit hole
- Keepalived高可用介绍与配置详解
- Shell函数、系统函数、basename [string / pathname] [suffix] 可以理解为取路径里的文件名称 、dirname 文件绝对路径、自定义函数
- 单核A7玩转人脸识别,恩智浦“跨界处理器”又玩出新花样!
- Build etcd distributed storage system +web management interface from scratch
- The UK and Germany have successively launched 5g commercial services, and Huawei has become a behind the scenes hero
- 7.25 simulation summary
- Easy gene | introduction to macrovirus sequencing technology
- [experiment sharing] CCIE BGP routing black hole experiment]
猜你喜欢

NVIDIA Canvas 初体验~

Chat software project development 2

BGP--边界网关协议

App uploader download and installation

Experiment 6 BGP federal comprehensive experiment

Buu brush inscription 3

81.(cesium之家)cesium修改灰色背景(默认蓝色)

Swiftui 4's new function of real-time access to click location.Ontapgeture {location in} (tutorial with source code)

Easycvr device management list page, paging data does not display problem repair

BGP routing black hole and anti ring
随机推荐
The UK and Germany have successively launched 5g commercial services, and Huawei has become a behind the scenes hero
执行上下文与词法环境
「企业管理」精诚CRM+——一体化管理企业业务流程
Can the training software test be employed
Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions
【微信小程序】零基础学 | 小程序语法
Leetcode's question brushing -- List summary
Swiftui 4's new function of real-time access to click location.Ontapgeture {location in} (tutorial with source code)
Kotlin - coroutinebuilder
Correct the classpath of your application so that it contains compatible versions of the classes com
Houdini 求中点,点连成线
[experiment sharing] CCIE BGP routing black hole experiment]
NVIDIA Canvas 初体验~
Buu brush inscription 1
Face recognition and fingerprint recognition are weak? Pentagon develops long-distance heartbeat recognition
Leetcode-300 最长递增子序列
实验5 OSPF综合实验
A super simple neural network code with 5 coordinates for one layer node training
Establishment of APP automation testing framework (VIII) -- establishment of ATX server2 multi device cluster environment
884. Uncommon words in two sentences - hash table