当前位置:网站首页>Day 013 一维数组练习
Day 013 一维数组练习
2022-07-27 23:20:00 【陌 年】
目录
2、求一个3*3矩阵对角线元素之和 <提示> 程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
3、有一个已经按升序排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。 <提示>程序分析:首先判断此数第一次小于数组中哪个元素,然后将此数插入,插入后此元 素之后的数,依次后移一个位置。
4、有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
1、 对10个整数进行按照从小到大的顺序排序
// 声明一个长度为10的数组
int[] nums = new int[10];
// 输入10个整数并存入数组中
Scanner sc = new Scanner(System.in);
System.out.println("请输入10个整数");
for (int i = 0; i < nums.length; i++) {
nums[i] = sc.nextInt();
}
// 冒泡排序
for (int i = 0; i < nums.length - 1; i++) {
for (int j = 0; j < nums.length - 1 - i; j++) {
if(nums[j+1]<nums[j]){
int temp = nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
sc.close();
2、求一个3*3矩阵对角线元素之和 <提示> 程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
// 定义一个二维数组
Scanner sc = new Scanner(System.in);
int[][] nums = new int[3][3];
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.println("请输入第" + (i + 1) + "行第" + (j + 1) + "个数:");
nums[i][j] = sc.nextInt();
}
}
// 求和
int sum = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
if (i == j || (i + j == 2 && i != j)) {
sum+=nums[i][j];
}
}
}
sc.close();
System.out.println("对角线之和是:"+sum);
3、有一个已经按升序排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
<提示>程序分析:首先判断此数第一次小于数组中哪个元素,然后将此数插入,插入后此元 素之后的数,依次后移一个位置。
// 定义一个升序排序的数组
int[] nums = { 12, 21, 23, 32, 34, 43, 45, 54 };
// 新创建一个数组,长度加1
int[] nums1 = new int[nums.length + 1];
// 将旧数组的值赋给新数组
for (int i = 0; i < nums.length; i++) {
nums1[i] = nums[i];
}
// 输入一个数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数据");
int number = sc.nextInt();
// 进行比较,确定数据插入的位置
int index = nums1.length - 1;
for (int i = 0; i < nums1.length; i++) {
if (number < nums1[i]) {
index = i;
break;
}
}
// 将插入位置之后的数据往后移动一个位置
for (int i = nums1.length-1; i>index ; i--) {
nums1[i] = nums1[i-1];
}
// 将插入的数据放入数组中
nums1[index] = number;
// 遍历输出
for (int i = 0; i < nums1.length; i++) {
System.out.print(nums1[i] + " ");
}
4、有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
//键盘录入n个整数
Scanner sc = new Scanner(System.in);
System.out.println("请输入整数的个数");
int n = sc.nextInt();
//创建一个数组,长度为n
int[] array = new int[n];
//依次输入数据并存入数组中
for (int i = 0; i < n; i++) {
System.out.print("输入第" + (i + 1) + "个数据");
array[i] = sc.nextInt();
}
//遍历输出数组
System.out.print("你输入的数组为:");
for (int i = 0; i < n; i++) {
System.out.print(array[i] + " ");
}
//输入向后位移的位数
System.out.print("\n请输入向后移动的位数:");
int m = sc.nextInt();
//重新定义一个数组,用来装填数组array中后m为数字
int[] b = new int[m];
//将数组array后m位数字赋值到数组b中
for (int i = 0; i < m; i++) {
b[i] = array[n - m + i];
}
//将数组array最后m位数字之前的数字赋值给最后几位
for (int i = n - 1; i >= m; i--) {
array[i] = array[i - m];
}
//将数组b中的值赋给数组array,因为数组b的长度为m,所以赋值的长度也为m,满足题意
for (int i = 0; i < m; i++) {
array[i] = b[i];
}
System.out.print("位移后的数组是:");
for (int i = 0; i < n; i++) {
System.out.print(array[i] + " ");
}
边栏推荐
- Huami technology "Huangshan No.2" release: AI performance is improved by 7 times, and power consumption is reduced by 50%!
- Xinyi information technology, a domestic NB IOT chip manufacturer, received 200million yuan of a+ round financing
- Swoole协程
- dataworks 传输数据到mysql 中文乱码是什么原因
- Arm中国夺权大战的背后:“独立”两年,仍难“自主”?
- Anfulai embedded weekly report no. 275: 2022.07.18--2022.07.24
- Wu xiongang sent an internal letter: arm's allegations are unwarranted, and no damage is allowed to the existing achievements!
- Fluent call interface UI
- BAT大厂测试架构师如何解读测试平台的各种争议
- 糟糕程序员的20个坏习惯
猜你喜欢

Redis cache penetration breakdown and avalanche

Codeforces暑期训练周报(7.21~7.27)

ICML2022 | 在线决策Transformer

Anfulai embedded weekly report no. 275: 2022.07.18--2022.07.24

Shutter -- password login registration interface

浏览器视频帧操作方法 requestVideoFrameCallback() 简介

【游戏】任天堂Nintendo Switch超详细购买/使用指南以及注意事项(根据自己使用持续更新中...)

Un7.13: how to add, delete, modify and query in vs Code?

Flutter 通话界面UI

Cesium add dynamic pop-up
随机推荐
浏览器视频帧操作方法 requestVideoFrameCallback() 简介
spreadsheet 导出 excel表格
In April, global smartphone shipments fell 41% year-on-year, and Huawei surpassed Samsung to become the world's first for the first time
ABAP CDS Table Function介绍与示例
Shutter -- password login registration interface
Brief analysis of advantages, disadvantages and development of SAP modules
The cooperation between starfish OS and metabell is just the beginning
对迁移学习中域适应的理解和3种技术的介绍
Huawei responded to the US blockade of the supply chain: they still have to pay for 5g patents
重新定义分析 - EventBridge 实时事件分析平台发布
Fabric2.4.4 version building process (complete process)
HarmonyOS 3正式发布:鸿蒙手机流畅安全,鸿蒙终端常用常新
Swoole定时器
华为旗下哈勃投资入股VCSEL芯片厂商纵慧芯光
ICML2022 | 在线决策Transformer
测试人员需要了解的软件流程
Wentai technology acquired the remaining equity of ANSYS semiconductor and obtained unconditional approval
Node red interacts with tdengine
Unity Shader入门精要学习——基础纹理
糟糕程序员的20个坏习惯