当前位置:网站首页>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] + " ");
}
边栏推荐
- ICML2022 | 在线决策Transformer
- Gazebo control example
- Huawei's Hubble investment shares in VCSEL chip manufacturer Zonghui Xinguang
- MySQL JPA support for JSON type data in database
- 在一个字符串里面统计给定字符串的个数
- Fluent call interface UI
- Use of swarm task task
- 如何让数字零售承接起流量时代和留量时代的发展重任,或许才是关键所在
- Uni app advanced style framework / production environment
- 4月全球智能手机出货同比下滑41%,华为首次超三星成全球第一
猜你喜欢

逻辑回归原理

Principle of logistic regression

Realize ABCD letter increment

Go 语言变量

氧气温湿度模组

From functional testing to automated testing, my monthly salary has exceeded 30k+, and I have 6 years of testing experience.

字节月薪28K,分享一波我的自动化测试经验....
![Deepening the concept of linear algebra [23] 01 - points coordinate points and vectors vectors](/img/9e/267ab60455e859afac3727eff32803.png)
Deepening the concept of linear algebra [23] 01 - points coordinate points and vectors vectors

Unity Shader入门精要学习——基础纹理

Focal Loss讲解
随机推荐
S-RPN: Sampling-balanced region proposal network for small crop pest detection
Token is used in nodejs
字节月薪28K,分享一波我的自动化测试经验....
Insider of container network hard core technology (7) sailing on the sea depends on the helmsman
Un7.13: how to add, delete, modify and query in vs Code?
2022/07/27 学习笔记 (day17) 代码块和内部类
If asynchronous processing is implemented according to the framework
激光器芯片厂商陕西源杰半导体获广发证券、中信证券等8家投资机构入股
Redis cache penetration breakdown and avalanche
Swoole collaboration
测试人员需要了解的软件流程
杂谈:一份最初就非常完善的FS跟第一版程序就要求没bug一样不切实际
The total investment is nearly 1.6 billion yuan! Qianzhao optoelectronics VCSEL and high-end LED chip projects officially started
线性代数 【23】 概念的深入01 - Points坐标点和Vectors向量
How to make digital retail undertake the development task of the era of traffic and retention may be the key
S-RPN: Sampling-balanced region proposal network for small crop pest detection
Codeforces暑期训练周报(7.14~7.20)
Fabric2.4.4 version building process (complete process)
Use of swarm task task
Harmonyos 3 was officially released: Hongmeng mobile phones are smooth and safe, and Hongmeng terminals are often used