实验五、数组
一、实验目的
- 掌握数组的概念与用法
二、实验设备与环境
装有JAVA语言工具软件 (Eclipse )的微机若干
三、实验内容
1、编写程序,完成以下功能:
1)输入20个整数到数组中;
2)使用冒泡法对20个数按从小到大排序,输出排序后的数组;
3)输入一个整数X,在数组中用二分法查找X,找到输出X在数组中的下标,找不到输出-1;
package com.xzj.day06;
import java.util.Scanner;
/**
* @author xuzhijie
* @create 2022-05-19 14:43
*/
public class Test01 {
public static void main(String[] args) {
int[] arr = new int[20];
System.out.println("生成的数组为: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 100);
System.out.print(arr[i] + " ");
}
System.out.println();
Scanner scn = new Scanner(System.in);
System.out.println("1:使用冒泡法对20个数按从小到大排序,输出排序后的数组;");
System.out.println("2:输入一个整数X,在数组中用二分法查找X,找到输出X在数组中的下标,找不到输出-1;");
int ren = scn.nextInt();
if (ren==1) {
Test01 t1 = new Test01();
t1.TestA(arr);
}
if (ren==2) {
Test01 t2 = new Test01();
System.out.println("\n请输入一个0~100的整数: ");
int number = scn.nextInt();
System.out.println("你要查找的数字的下标是:" + t2.TestB(arr, number));
}
}
public void TestA(int[] arr) {
int temp;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("\n排序后的顺序: ");
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
public int TestB(int[] arr,int number){
int top = 0;
int rear = arr.length - 1;
int mid = (top+rear)/2;
int temp;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
if(number > arr[arr.length-1] || number < arr[0]){
return -1;
}
while(top <= rear){
mid = (top + rear) / 2;
if(arr[mid] > number){
rear = mid - 1;
}else if(arr[mid] < number){
top = mid + 1;
}else{
return mid;
}
}
return -1;
}
}
- 找出一个二维数组的鞍点,即该位置上的元素在该行上最大、在列上最小(也可能没有鞍点。)
package com.xzj.day07;
import java.util.Scanner;
/**
* @author xuzhijie
* @create 2022-06-01 16:35
*/
public class Test01{
public static void main(String[] args) {
int[][] matrix = new int[5][5];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.println("请输入 matrix[" + i + "][" + j + "]");
matrix[i][j] = scanner.nextInt();
}
}
int maxY = 0;
int maxX = 0;
for (int i = 0; i < 5; i++) {
maxX = matrix[i][0];
boolean flag = true;
for (int j = 1; j < 5; j++) {
if (maxX < matrix[i][j]) {
maxX = matrix[i][j];
maxY = j;
}
}
for (int j = 1; j < 5; j++) {
if (maxX < matrix[j][maxY]) {
{
flag = false;
break;
}
}
}
if (flag) {
System.out.println("靶点位置:matxix[" + i + "][" + maxY + "]:" + maxX);
}
}
}
}
- 使用数组编程,实现打印输出 Fibonacci 数列的前 60 项。Fibonacci 数列的前 60 项的结果及格式如下:
package com.xzj.day08;
/**
* @author xuzhijie
* @create 2022-06-01 16:45
*/
public class Test01 {
public static void main(String[] args) {
int arr[] = new int[60];
arr[0] = arr[1] = 1;
for (int i = 2; i < arr.length; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
System.out.println("斐波那契数列的前60项如下所示:");
for (int i = 0; i < arr.length; i++) {
if (i % 5 == 0)
System.out.println();
System.out.printf("%15s",arr[i]);
}
}
}