当前位置:网站首页>Day012 一维数组的应用
Day012 一维数组的应用
2022-07-27 05:24:00 【陌 年】
有一组学员的成绩{99,85,82,63, 60},将它们按降序排列。
要增加一个学员的成绩,将它插入成绩序列,并保持降序.
思路:
1)新建一个比原来数组长度大1的数组
2)将原来数组里面的元素按照顺序存储到新的数组中
3)使用Scanner获取增加学员的成绩
4)通过循环比较获取插入元素要插入的位置
5)从插入位置开始及之后的元素依次
int[] scores = { 99, 85, 82, 63, 60 };
// 1)新建一个比原来数组长度大1的数组
int[] newScores = new int[scores.length + 1];
// 2)将原来数组里面的元素按照顺序存储到新的数组中
for (int i = 0; i < scores.length; i++) {
newScores[i] = scores[i];
}
// 3)使用Scanner获取增加学员的成绩
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要增加的学员成绩:");
int insert = sc.nextInt();
// 4)通过循环比较获取插入元素要插入的位置
int index=newScores.length-1;
for (int i = 0; i < newScores.length; i++) {
if(insert>newScores[i]){
index=i;
// 一旦获取元素符合条件,就不再比较
break;
}
}
// 5)从插入位置及之后的元素依次往后移动一位(注意:移动的时候,从后往前移动;下标从0开始的)
for(int i = newScores.length-1;i>index;i--){
newScores[i]=newScores[i-1];
}
// 6)将插入的元素放到相应的位置中
newScores[index]=insert;
// 遍历
for(int i=0;i<newScores.length;i++){
System.out.print(newScores[i]+" ");
}
边栏推荐
- PSI|CSI和ROC|AUC和KS -备忘录
- Sunflower teaches you how to prevent denial of service attacks?
- DNS domain name resolution service
- darknet-yolov3、yolo-fastect使用rtx30系显卡cuda环境在win10平台编译与训练的相关问题
- Some applications of std:: bind and std:: function
- Future, futuretask and completable future are often asked in interviews
- Redis operation of Linux Installation
- Speech and language processing (3rd ed. draft) Chapter 2 - regular expression, text normalization, editing distance reading notes
- System security and Application
- Log in to Alibaba cloud server with a key
猜你喜欢

云原生运行环境搭建

After adding a camera (camera) to the UAV in gazebo, the UAV cannot take off

torch加载自定义模型的问题

事件捕获方式和冒泡方式—它们的区别是什么?

PSI|CSI和ROC|AUC和KS -备忘录
![Summary of frequently asked questions in the interview [summarized after painstaking work all night]](/img/70/90543c23326305590242716b989643.png)
Summary of frequently asked questions in the interview [summarized after painstaking work all night]

Linux安装Redis操作

众多世界500强企业集聚第二届数博会,数字产业大幕即将开启!

关于卡尔曼滤波的协方差如何影响deepsort的跟踪效果的考虑

FTX US推出FTX Stocks,向主流金融行业迈进
随机推荐
Common font and color settings of markdown documents
Sunflower teaches you how to prevent denial of service attacks?
where接自定义函数导致查询缓慢
Problems related to compilation and training of Darknet yolov3 and Yolo fast using CUDA environment of rtx30 Series graphics card on win10 platform
pycharm在虚拟环境下跑jupyter notebook问题记录
win10 添加虚拟网卡,配置op路由
DNS domain name resolution service
How to delete or replace the loading style of easyplayer streaming media player?
px4源码编译之 建立自己的程序模块
FTX US推出FTX Stocks,向主流金融行业迈进
System security and Application
NFS introduction and configuration
ESXI虚拟机启动,模块“MonitorLoop”打开电源失败
众多世界500强企业集聚第二届数博会,数字产业大幕即将开启!
Explanation of server related indicators
shell常用命令-备忘录
Numpy array and image conversion
TS学习(八) :TS中的类
NAT(网络地址转换)
磁盘管理与文件系统