写在前面
继续昨天Java中的数组和方法部分的习题,今天写十题编程题,来看看你能写出来几题。答案也是仅供参考,如果有更好的解法欢迎在下面留言!
题目展示
1.数组查找操作:定义一个长度为10 的一维字符串数组,在每一个元素存放一个单词;然后运行时从命令行输入一个单词,程序判断数组是否包含有这个单词,包含 这个单词就打印出“Yes”,不包含就打印出“No”。
代码:
public static void main(String[] args) {System.out.println("请输入一个单词:");Scanner sc = new Scanner(System.in);String word = sc.next();String[] array = new String[]{"first","time","you","sing","song","got","practice","same","principle","applies"};for(int i = 0; i < array.length;i++){if(word.equals(array[i])){System.out.println("Yes");return;}}System.out.println("No");}
结果截图:
2.数组逆序操作:定义长度为10的数组,将数组元素对调,并输出对调前后的结果。 思路:把0索引和arr.length-1的元素交换,把1索引和arr.length-2的元素交换,只要交换到arr.length/2的时候即可。
代码:
public static void main(String[] args) {System.out.println("逆序前的数组为:");char[] array = new char[]{'a','b','c','d','e','f','g','h','i','j'};printArray(array);for (int start=0, end = array.length-1 ; start < end ; start++,end--){char temp = array[start];array[start] = array[end];array[end]= temp;}System.out.println("逆序后的数组为:");printArray(array);}public static char[] printArray(char[] arr){for (int k = 0 ; k < arr.length ; k++){if(k==0){System.out.print("["+arr[k]+", ");}else if(k == arr.length-1){System.out.print(arr[k]+"]");}else {System.out.print(arr[k]+", ");}}System.out.println();return arr;}
结果截图:
3.合并数组操作:现有如下一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: int newArr [] ={1,3,4,5,6,6,5,4,7,6,7,5}
代码:
public static void main(String[] args) {int[] oldArr = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};System.out.println("原数组为:");printArray(oldArr);int count = 0;for (int i = 0 ; i<oldArr.length ; i++){if(oldArr[i] != 0){count++;}}int[] newArr = new int[count];int size = 0;for (int i = 0 ; i< oldArr.length;i++){if(oldArr[i] != 0){newArr[size] = oldArr[i];size++;}}System.out.println("去除0之后的数组为:");printArray(newArr);}public static int[] printArray(int[] arr){for (int k = 0 ; k < arr.length ; k++){if(k==0){System.out.print("["+arr[k]+", ");}else if(k == arr.length-1){System.out.print(arr[k]+"]");}else {System.out.print(arr[k]+", ");}}System.out.println();return arr;}
结果截图:
4.二分法查找操作:使用二分法查找有序数组中元素。找到返回索引,不存在输出-1。 分析:二分法查找的前提是数组有序。
假如有一组数为3,12,24,36,55,68,75,88要查给定的值24.
可设三个变量front,mid,end分别指向数据的上界,中间和下界,mid=(front+end)/2.
1)开始令front=0(指向3),end=7(指向88),则mid=3(指向36)。因为mid>x, 故应在前半段中查找。
2)令新的end=mid-1=2,而front=0不变,则新的mid=1。此时x>mid,故确定应 在后半段中查找。
3)令新的front=mid+1=2,而end=2不变,则新mid=2,此时a[mid]=x,查找成功。
4)如要查找的数不是数列中的数,例如x=25,当第三次判断时,x>a[mid],按以上规律,令front=mid+1,即front=3,出现front>end的情况,表示查找不成功。
代码:
public static void main(String[] args) {int[] a = new int[]{3, 36, 55, 68, 75, 88, 12, 24};int[] orderAsc = maoPaoOrder(a);System.out.println("升序排序好的数组为:");printArray(orderAsc);Scanner sc = new Scanner(System.in);System.out.println("请输入一个您要查找的数:");int number = sc.nextInt();int front = 0;int end = a.length - 1;int res = -1;while (front <= end) {int mid = (front + end) / 2;if (number > a[mid]) {front = mid + 1;} else if (number < a[mid]) {end = mid - 1;} else {res = mid;break;}}if (res == -1) {System.out.println(-1);} else {System.out.println("查找成功!你要查找的数的索引为:" + res);}}public static int[] maoPaoOrder(int[] arr) {for (int i = 0; i < arr.length; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] > arr[j]) {int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}return arr;}public static int[] printArray(int[] arr) {for (int k = 0; k < arr.length; k++) {if (k == 0) {System.out.print("[" + arr[k] + ", ");} else if (k == arr.length - 1) {System.out.print(arr[k] + "]");} else {System.out.print(arr[k] + ", ");}}System.out.println();return arr;}
结果截图:
5.二维数组遍历求和操作:用二重循环求出二维数组b所有元素的和:int[][] b={{11},{21,22},{31,32,33}}。
代码:
public static void main(String[] args) {int[][] b = {{11}, {21, 22}, {31, 32, 33}};int sum = 0;for (int i = 0; i < b.length; i++) {for (int j = 0; j < b[i].length; j++) {sum = sum + b[i][j];}}System.out.println("二维数组b的元素和为:" + sum);}
结果截图:
*6.题目:输入某年某月某日,判断这一天是这一年的第几天?分析:以3月5日为例,先把前两个月的加起来,然后再加上5天即本年第几天,特殊情况,闰年且输入月份大于3需考虑多加一天。可定义数组存储1-12月各月天数。 *
代码:
public static void main(String[] args) {int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int year = 0;int month = 0;int day = 0;int sum = 0;System.out.println("请输入日期,包含年月日,两个数之间用空格分隔:");Scanner sc = new Scanner(System.in);year = sc.nextInt();month = sc.nextInt();day = sc.nextInt();if ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0) {days[1] = 29;}for (int i = 0; i < month - 1; i++) {sum = sum + days[i];}sum = sum + day;System.out.println(year + "年" + month + "月" + day + "日对应这一年的第" + sum + "天。");}
结果截图:
7.使用二分法查找有序数组中元素。找到返回索引,不存在输出-1。使用递归实现。
代码:
public static void main(String[] args) {int[] a = new int[]{3, 36, 55, 68, 75, 88, 12, 24};int[] orderAsc = maoPaoOrder(a);System.out.println("升序排序好的数组为:");printArray(orderAsc);Scanner sc = new Scanner(System.in);System.out.println("请输入一个您要查找的数:");int number = sc.nextInt();int res = erFenSearch(a, number);if (res == -1) {System.out.println("查找失败!" + -1);} else {System.out.println("查找成功!你要查找的数的索引为:" + res);}}//二分查找的结果方法public static int erFenSearch(int[] arr, int x) {return erFenSearch(arr, x, 0, arr.length - 1);}//二分查找的方法public static int erFenSearch(int[] arr, int x, int front, int end) {int mid = (front + end) / 2;while (front <= end) {if (x > arr[mid]) {return erFenSearch(arr, x, mid + 1, end);} else if (x < arr[mid]) {return erFenSearch(arr, x, front, mid - 1);} else {return mid;}}return -1;}//冒泡排序数组public static int[] maoPaoOrder(int[] arr) {for (int i = 0; i < arr.length; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] > arr[j]) {int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}return arr;}//打印数组方法public static int[] printArray(int[] arr) {for (int k = 0; k < arr.length; k++) {if (k == 0) {System.out.print("[" + arr[k] + ", ");} else if (k == arr.length - 1) {System.out.print(arr[k] + "]");} else {System.out.print(arr[k] + ", ");}}System.out.println();return arr;}
结果截图:
8.数组A:1,7,9,11,13,15,17,19;数组b:2,4,6,8,10 两个数组合并为数组c,按升序排列。
代码:
public static void main(String[] args) {int[] a = new int[]{1, 7, 9, 11, 13, 15, 17, 19};System.out.println("数组a为:");printArray(a);int[] b = new int[]{2, 4, 6, 8, 10};System.out.println("数组b为:");printArray(b);int[] c = new int[a.length + b.length];int size = 0;for (int i = 0; i < c.length; i++) {if (i < a.length) {c[i] = a[i];} else if (i >= a.length) {c[i] = b[size];size++;}}System.out.println("合并后的数组c为:");printArray(c);int[] res = maoPaoOrder(c);System.out.println("排序后的数组c为:");printArray(res);}//冒泡排序数组的方法public static int[] maoPaoOrder(int[] arr) {for (int i = 0; i < arr.length; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] > arr[j]) {int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}return arr;}//打印数组的方法public static int[] printArray(int[] arr) {for (int k = 0; k < arr.length; k++) {if (k == 0) {System.out.print("[" + arr[k] + ", ");} else if (k == arr.length - 1) {System.out.print(arr[k] + "]");} else {System.out.print(arr[k] + ", ");}}System.out.println();return arr;}
结果截图:
9.字符大小转换:输入一个字符,判断它是否为小写字母,如果是,将它转换成大写字母,否则,不转换;
代码:
public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入一个英文字母:");char c = sc.next().charAt(0);if (65 <= c && c <= 90) {c += 32;System.out.println("转为小写字母为:"+c);} else if (97 <= c && c <= 122) {c -= 32;System.out.println("转为大写字母为:"+c);}else {System.out.println("输入无效,请输入a-z任意一个字母(不分大小写)再试!");}char res1 = upperLower(c);System.out.println("转换后:" + res1);}public static char upperLower(char a ){if (65 <= a && a <= 90) {a += 32;return a;} else if (97 <= a && a <= 122) {a -= 32;return a;}return '0';}
结果截图:
10.打印杨辉三角。
代码:
public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入要打印的杨辉三角的行数:");int number = sc.nextInt();int[][] array = new int[number][number];for (int i = 0; i < number; i++) {array[i][0] = 1;}for (int j = 1; j < number; j++) {for (int k = 1; k <= j; k++) {array[j][k] = array[j - 1][k - 1] + array[j - 1][k];}}for (int i = 0; i < number; i++) {for (int j = i; j < number - 1; j++) {System.out.print(" ");}for (int k = 0; k <= i; k++) {System.out.print(array[i][k] + " ");}System.out.println();}}
结果截图:
欢迎大家在下面评论出你的想法,如有错误还请批评指正。