当前位置:网站首页>Daily practice ----- there is a group of students' grades. Arrange them in descending order. To add a student's grade, insert it into the grade sequence and keep the descending order
Daily practice ----- there is a group of students' grades. Arrange them in descending order. To add a student's grade, insert it into the grade sequence and keep the descending order
2022-07-26 21:00:00 【Beiningmo language】
subject : There is a group of students' achievements {99,82,85,60, 63}, Arrange them in descending order . To increase the performance of a student , Insert it into the grade sequence , And keep descending
The key to solving the problem is : Descending order , After increasing students' scores, they still need to be arranged in descending order
Ideas : 1) First arrange the original array in descending order
2) Create a new array that is longer than the original array 1 Array of
3) Store the elements in the original array in order in the new array
4) Use Scanner Get the student grades you want to increase
5) Through cyclic comparison , Get the position where the inserted element is to be inserted ( Subscript )
6) The elements starting from and after the insertion position move one bit back ( Be careful : When moving , Move forward from behind )
7) After moving the element , The insertion position is empty , Store the inserted element in the insertion location
The process : Next, we write code step by step according to our problem-solving ideas
// Output the array required by the topic
int[] scores = { 99, 82, 80, 60, 63 };
//1) First arrange the original array in descending order
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;
}
}
}
// Traversal array , See if it succeeds in descending order
System.out.println(" The descending array is :");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
}

//2) Create a new array that is longer than the original array 1 Array of
int[] newScores = new int[scores.length + 1];
// 3) Store the elements in the original array in order in the new array
for (int i = 0; i < scores.length; i++) {
newScores[i] = scores[i];
}
System.out.println();
// 4) Use Scanner Get the student grades you want to increase
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the increased student grade ");
int insert = sc.nextInt();
// 5) Through cyclic comparison , Get the position where the inserted element is to be inserted ( Subscript )
int index = newScores.length - 1; // Prevent mistakes when the score is negative
for (int i = 0; i < newScores.length; i++) {
if (insert > newScores[i]) {
index = i;
// Once the inserted element is larger than a certain element , No further comparison
break;
}
}
// 6) The elements starting from and after the insertion position move one bit back ( Be careful : When moving , Move forward from behind )
for (int i = newScores.length - 1; i > index; i--) {
newScores[i] = newScores[i - 1];
}
// 7) After moving the element , The insertion position is empty , Store the inserted element in the insertion location
newScores[index] = insert;
// After inserting the element , Traverse the new array
System.out.println(" The new array in descending order after inserting grades is :");
for (int j = 0; j < newScores.length; j++) {
System.out.print(newScores[j] + " ");
}
The complete results are as follows :


summary :
This program mainly uses the knowledge of array loops , Part of the program is " int index = newScores.length - 1;" If written "int index = 0", And the inserted score is negative , Then the following results will appear :

So the assignment of this requires extra attention .
For the convenience of everyone , The source code is attached below :
int[] scores = { 99, 85, 63, 60, 82 };
//1) First arrange the original array in descending order
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;
}
}
}
// Output the descending array
System.out.println(" The descending array is :");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
}
// 2) Create a new array that is longer than the original array 1 Array of
int[] newScores = new int[scores.length + 1];
// 3) Store the elements in the original array in order in the new array
for (int i = 0; i < scores.length; i++) {
newScores[i] = scores[i];
}
System.out.println();
// 4) Use Scanner Get the student grades you want to increase
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the increased student grade ");
int insert = sc.nextInt();
// 5) Through cyclic comparison , Get the position where the inserted element is to be inserted ( Subscript )
int index = 0;
for (int i = 0; i < newScores.length; i++) {
if (insert > newScores[i]) {
index = i;
// Once the inserted element is larger than a certain element , No further comparison
break;
}
}
// 6) The elements starting from and after the insertion position move one bit back ( Be careful : When moving , Move forward from behind )
for (int i = newScores.length - 1; i > index; i--) {
newScores[i] = newScores[i - 1];
}
// 7) After moving the element , The insertion position is empty , Store the inserted element in the insertion location
newScores[index] = insert;
// After inserting the element , Traverse the new array
System.out.println(" The new array in descending order after inserting grades is :");
for (int j = 0; j < newScores.length; j++) {
System.out.print(newScores[j] + " ");
}Practice tomorrow : Realize the lottery function of two-color ball . The rules : from 36 Randomly choose the non repeating one among the red balls 6 Number , from 15 Randomly choose from basketball 1 Make up a lottery . You can choose to buy more notes .
You can write by yourself , Tomorrow noon 12 I'll send my writing on time , Tomorrow, 12 I'll see you when I see you
边栏推荐
- Pspice simulation quartz crystal oscillation circuit
- 详细图解b树及C语言实现
- 拦截器、、
- Buu brush inscription 3
- Redis面试题
- Use Baidu PaddlePaddle easydl to complete garbage classification
- Can the training software test be employed
- 手机\固定电话座机呼叫转移设置方法
- Correlation analysis between [machine learning] variables
- LeetCode链表问题——19.删除链表的倒数第N个节点(一题一文学会链表)
猜你喜欢

易基因|宏病毒组测序技术介绍

09_ UE4 advanced_ Enter the next level and reserve the blood volume

Beginner experience of safety testing

Buu brush inscription - WANGDING cup column 2
![[experiment sharing] CCIE BGP routing black hole experiment]](/img/c8/7ccb879ad7c739d3573637fd14f4e1.png)
[experiment sharing] CCIE BGP routing black hole experiment]

拦截器、、
![[OBS] solve the problem of OBS pushing two RTMP streams + timestamp](/img/71/dbd00f69251b96b0e56de399103f72.png)
[OBS] solve the problem of OBS pushing two RTMP streams + timestamp

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

JDBC的连接

flask 源码梗概
随机推荐
[ffmpeg] add timestamp summary to video files
Transaction rollback and record exception information at the same time
ST表、带权并查集
Explain the 190 secondary compiler (decoding table) module of SMR laminated hard disk of Western data in detail
Establishment of APP automation testing framework (VIII) -- establishment of ATX server2 multi device cluster environment
How to check whether the pytorch you are using is a GPU version
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
Applied materials announced that it would acquire international electric with us $2.2 billion in cash
Can the training software test be employed
Discussion on loan agreement mode with NFT as collateral
Leetcode array class
Kotlin - coroutinecontext
[英雄星球七月集训LeetCode解题日报] 第26日 并查集
执行上下文与词法环境
游览器——游览器游览器缓存
LeetCode链表问题——24.两两交换链表中的结点(一题一文学会链表)
Tkinter uses WPF controls
Centos7 about Oracle RAC 11gr2 deployment disk partition
"Enterprise management" sincere crm+ - integrated management of enterprise business processes
[pyqt5 basic control usage analysis]