目录
1. 输入10位同学的java成绩:(1)求平均成绩,最高成绩、最低成绩;(2)对10的成绩按照从低到高的顺序排列(选做)
3. 给定一个数组,大小为10,输入十个同学的成绩,求这些成绩的总分数;某一天转入2个新同学,请再次求12个同学的平均成绩(尝试着使用扩容的形式做)
4. 有一堆硬币,每次只能拿一个或者两个,求最少多少次可以拿完硬币
7. 存在整数数组nums,将该数组中的所有偶数元素排列到奇数元素前
9. 某个人进入如下一个棋盘中,要求从左上角开始走,最后从右下角出来(要求只能前进,不能后退),问题:共有多少种走法?
- import java.util.Arrays;
- import java.util.Scanner;
-
- /*
- * 请输入10位同学的java成绩,
- * 1、求平均成绩,最高成绩、最低成绩
- * 2、对10的成绩按照从低到高的顺序排列(选做)
- */
- public class Test1 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("请依次输入10位同学的java成绩:");
- int[] arr = new int[10];
- for (int i = 0; i < arr.length; i++) {
- arr[i] = scanner.nextInt();
- }
- // 68 75 58 71 89 77 88 91 49 66
- System.out.print("十位同学的java成绩为:");
- System.out.println(Arrays.toString(arr));
- float averScore = findAverScore(arr);
- System.out.println("平均成绩为:" + averScore); // 73.2
- int maxScore = findMaxScore(arr);
- System.out.println("最大成绩为:" + maxScore); // 91
- int minScore = findMinScore(arr);
- System.out.println("最小成绩为:" + minScore); // 49
-
- // 定义一个排序函数:对10的成绩按照从低到高的顺序排列
- // bubbleSort(arr); // 冒泡排序
- // selectSort(arr); // 选择排序
- insertSort(arr); // 插入排序
- System.out.println(Arrays.toString(arr));
- }
-
- public static void insertSort(int[] arr) {
- // TODO Auto-generated method stub
- for (int i = 0; i < arr.length - 1; i++) {
- for (int j = i + 1; j > 0; j--) {
- if (arr[j] < arr[j - 1]) {
- swap(arr, j, j - 1);
- }
- }
- }
- }
-
- public static void selectSort(int[] arr) {
- // TODO Auto-generated method stub
- for (int i = 0; i < arr.length - 1; i++) {
- for (int j = i + 1; j < arr.length; j++) {
- if (arr[j] < arr[i]) {
- swap(arr, j, j - 1);
- }
- }
- }
- }
-
- public static void bubbleSort(int[] arr) {
- // TODO Auto-generated method stub
- for (int i = 0; i < arr.length - 1; i++) {
- for (int j = 0; j < arr.length - 1; j++) {
- if (arr[j] > arr[j + 1]) {
- swap(arr, j, j + 1);
- }
- }
- }
- }
-
- public static void swap(int[] arr, int i, int j) {
- // 异或法交互两个变量
- arr[i] = arr[i] ^ arr[j];
- arr[j] = arr[i] ^ arr[j];
- arr[i] = arr[i] ^ arr[j];
- }
-
- public static int findMinScore(int[] arr) {
- // TODO Auto-generated method stub
- int minScore = arr[0];
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] < minScore) {
- minScore = arr[i];
- }
- }
- return minScore;
- }
-
- public static int findMaxScore(int[] arr) {
- // TODO Auto-generated method stub
- int maxScore = arr[0];
- for (int i = 1; i < arr.length; i++) {
- if (arr[i] > maxScore) {
- maxScore = arr[i];
- }
- }
- return maxScore;
- }
-
- public static float findAverScore(int[] arr) {
- // TODO Auto-generated method stub
- float averScore;
- float sum = 0;
- for (int i = 0; i < arr.length; i++) {
- sum += arr[i];
- }
- averScore = sum / arr.length;
- return averScore;
- }
- }
结果:
- import java.util.Arrays;
- import java.util.Scanner;
-
- public class Test2 {
- public static void main(String[] args) {
- /*
- * 给定一个数组,判断某个元素是否在该数组中
- */
- int[] arr = {68, 75, 58, 71, 89, 77, 88, 91, 49, 66};
- Scanner scanner = new Scanner(System.in);
- System.out.print("请输入一个要查找的数:");
- int target = scanner.nextInt();
- int index = binarySearch(arr, 0, arr.length - 1, target);
- if (index == -1) {
- System.out.println("很遗憾,你要找的数不在数组中!");
- }else {
- System.out.println("你要找的数在数组中!");
- }
- }
-
- public static int binarySearch(int[] arr, int start, int end, int target) {
- // 二分查找前提是要让arr有序
- Arrays.sort(arr);
- int mid = (start + end) >> 1;
- if (start <= end) {
- if (arr[mid] == target) {
- return mid;
- }else if (arr[mid] < target) {
- start = mid + 1;
- return binarySearch(arr, start, end, target);
- }else {
- end = mid - 1;
- return binarySearch(arr, start, end, target);
- }
- }
- return -1;
- }
- }
结果:
- import java.util.Arrays;
- import java.util.Scanner;
-
- public class Test3 {
- /*
- * 给定一个数组,大小为10,输入十个同学的成绩,求这些成绩的总分数;
- * 某一天转入2个新同学,请再次求12个同学的平均成绩(尝试着使用扩容的形式做)
- */
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入10个同学的成绩:");
- int[] arr = new int[10];
- for (int i = 0; i < arr.length; i++) {
- arr[i] = scanner.nextInt();
- }
- int sum = sumScore(arr);
- System.out.println("总成绩为:" + sum);
- // 新转来两个,数组扩容
- arr = expandCapacity(arr);
- System.out.println(Arrays.toString(arr));
- sum = sumScore(arr);
- float aver = sum / arr.length;
- System.out.println("平均值为:" + aver);
- }
-
- public static int[] expandCapacity(int[] arr) {
- // 数组扩容
- Scanner scanner = new Scanner(System.in);
- System.out.print("请输入两个新同学的成绩:");
- int[] arr1 = new int[arr.length + 2];
- for (int i = 0; i < arr1.length; i++) {
- if (i < arr.length) {
- arr1[i] = arr[i];
- }else {
- arr1[i] = scanner.nextInt();
- }
- }
- return arr1;
- }
-
- public static int sumScore(int[] arr) {
- // 求和
- int sum = 0;
- for (int i = 0; i < arr.length; i++) {
- sum += arr[i];
- }
- return sum;
- }
- }
结果:
桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。(LeetCode)
- public class Test4 {
- /*
- * ·有一堆硬币,每次只能拿一个或者两个,求最少多少次可以拿完硬币
- * {10, 8, 5, 3, 27, 99}
- */
- public static void main(String[] args) {
- int[] coins = {10, 8, 5, 3, 27, 99};
- int result = minCount(coins);
- System.out.println("最少" + result + "次可以拿完硬币");
- }
-
- public static int minCount(int[] coins) {
- int count = 0;
- for(int i = 0; i < coins.length; i++) {
- if(coins[i] % 2 == 0) {
- count += (coins[i] / 2);
- }else{
- count += (coins[i] / 2 + 1);
- }
- }
- return count;
- }
- }
结果:
笨方法,双层循环
- package com.openlab.day09.exer;
-
- import java.util.Arrays;
-
- public class Test5 {
- // 将数组中的重复数据去重
- public static void main(String[] args) {
- int[] arr = {5, 4 ,9, 5, 2, 5, 8, 4};
- arr = removeRepeat(arr);
- System.out.println(Arrays.toString(arr));
- }
-
- public static int[] removeRepeat(int[] arr) {
- // 将数组中的重复元素去除
- int[] temp = new int[arr.length];
- int count = 0;
- boolean isFlag = true;
- for (int i = 0; i < temp.length; i++) {
- for (int j = 0; j < temp.length; j++) {
- if (temp[j] == arr[i]) {
- isFlag = false;
- }
- }
- if (isFlag) {
- temp[count++] = arr[i];
- }
- isFlag = true;
- }
- int[] newArr = new int[count];
- for (int i = 0; i < newArr.length; i++) {
- newArr[i] = temp[i];
- }
- return newArr;
- }
- }
结果:
- public class Test6 {
- // 如何将10个0-10随机存入数组中
- public static void main(String[] args) {
- int[] arr = new int[10];
- putRandomArray(arr);
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + "\t");
- }
- }
-
- private static void putRandomArray(int[] arr) {
- // 随机存入数组
- for (int i = 0; i < arr.length; i++) {
- int randomNum = (int)(Math.random() * 11);
- arr[i] = randomNum;
- }
- }
- }
结果:
- import java.util.Arrays;
-
- public class Test7 {
- public static void main(String[] args) {
- int[] nums = {1, 4, 5, 3, 6, 7, 10};
- exchange(nums);
- System.out.println(Arrays.toString(nums));
- }
-
- public static void exchange(int[] nums) {
- int i = 0, j = nums.length - 1;
- while(i < j) {
- while(nums[i] % 2 == 0) {
- i++;
- }
- while(nums[j] % 2 == 1) {
- j--;
- }
- if (i < j) {
- swap(nums, i, j);
- }
- }
- }
-
- public static void swap(int[] nums, int i, int j) {
- // 交换数组中两个元素的值
- nums[i] = nums[i] ^ nums[j];
- nums[j] = nums[i] ^ nums[j];
- nums[i] = nums[i] ^ nums[j];
- }
- }
结果:
- public class Test {
- public static void main(String[] args) {
- String s1 = "HelloWorld";
- String s2 = new String("HelloWorld");
- if (s1 == s2) {
- System.out.println("s1 == s2");
- }else {
- System.out.println("s1 != s2");
- }
- if (s1.equals(s2)) {
- System.out.println("s1 equals s2");
- } else {
- System.out.println("s1 not equals s2");
- }
- }
- }
A. s1 == s2
s1 not equals s2
B. s1 == s2
s1 equals s2
C. s1 != s2
s1 not equals s2
D. s1 != s2
s1 equals s2
解析:字符串是一个对象,在java中 == 判断的是值是否相等,不能用来判断字符串,因为字符串的创建方式有多种,不同的创建方式,地址(指针) 可能不一致。在java中 == 判断的是值是否相等,不能用来判断字符串,字符串中 == 判断的字符串的地址,equals判断是字符串的值;s1和s2是两个不同的字符串对象,所以地址不一样,所以s1 != s2;s1 equals s2,故选D。
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
暂时不会