收藏 分销(赏)

java经典基础练习题及参考答案.docx

上传人:仙人****88 文档编号:12010341 上传时间:2025-08-27 格式:DOCX 页数:12 大小:41.80KB 下载积分:10 金币
下载 相关 举报
java经典基础练习题及参考答案.docx_第1页
第1页 / 共12页
java经典基础练习题及参考答案.docx_第2页
第2页 / 共12页


点击查看更多>>
资源描述
1. 写一个函数,计算一个整数数组的平均值 import java.util.Random; public class javaSevenDayLianXi { public static void main(String[] args) { int arr[] = new int[10]; Random score = new Random(); float sum = 0.0f; System.out.print("这组数为:"); for (int i = 0; i < arr.length; i++) { arr[i] = score.nextInt(101); sum += arr[i]; System.out.print(arr[i] + " "); } System.out.println(); System.out.println("数组arr的平均值为:" + sum / arr.length); } } 2. 自定义一个整数数组a,读入一个整数n,如果n 在数组中存在,则输出n 的下标;如果 不存在,则输出-1。 Scanner sca = new Scanner(System.in); int []a = {0,1,2,3,4,5,6,7,8,9}; System.out.print("请输入一个数:"); int shuJu = sca.nextInt(); boolean bool = true; for(int i=0;i<a.length;i++){ if(a[i]==shuJu){ System.out.println(shuJu+"在数组a中的下标为:"+i); bool = false; break; } } if(bool) System.out.println(-1); 3. 给定一个数组,输出数组中的最大值和最小值 int []a = {12,34,563,2,45,778,554,4421,4456,6786}; Arrays.sort(a); int min=a[0]; int max=a[a.length-1]; System.out.println("数组中最小的数为:"+min+",最大的数为:"+max); 4. *给定一个数组,把这个数组中所有元素顺序进行颠倒。 import java.util.Random; public class javaSevenDayLianXi { public static void main(String[] args) { int arr[] = new int[10]; int shu=0; Random score = new Random(); System.out.print("颠倒前的arr组数值为:"); for (int i = 0; i < arr.length; i++) { arr[i] = score.nextInt(101); System.out.print(arr[i] + " "); } for(int i=0;i<=(arr.length-1)/2;i++){ shu=arr[i]; arr[i]=arr[arr.length-1-i]; arr[arr.length-1-i]=shu; } System.out.println(); System.out.print("颠倒后的arr数组值为:"); for(int i :arr) System.out.print(i+" "); } } 5. *数组的扩容。 给定一个数组,要求写一个expand 函数,把原有数组的长度扩容一倍,并保留原有数 组原有的内容。 例如,给定一个数组int[] a = {1,2,3},则扩容之后,a 数组为:{1,2,3,0,0,0} public class javaNineDay { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 }; arr = setArrays(arr); for (int term : arr) System.out.print(term + " "); } public static int[] setArrays(int[] arr) { int[] arrays = new int[arr.length * 2]; System.arraycopy(arr, 0, arrays, 0, arr.length); return arrays; } } 6. *数组的插入和删除 写两个函数,一个函数为delete 函数,声明如下: public static void delete(int pos) 该函数表示删除数组pos 位置上的元素。 第二个函数为insert 函数,声明如下: public static void insert(int pos, int value) 该函数表示在数组pos 位置上插入value 值。 为了能在多个函数中使用同一个数组,需要把这个数组写在函数的外面,类的里面,并 使用static 修饰。 为了方便,还应该定义一个index 变量,用来保存数组的有效元素的个数。 例如下面的代码: public class TestInsertDelete{ static int[] a = {1,3,2,5,7}; //多个函数可以直接操作该数组 static int index = 5; public static void main(String args[]){ delete(2); //1 3 5 7 insert(1, 4); //1 4 3 5 7 insert(0, 6); //6 1 4 3 5 7 } public static void delete(int pos){ … } public static void insert(int pos, int value){ … } } 实现方式: 1. delete 方法:把数组pos 位后的所有元素向前移动1 位 2. insert 方法:把数组pos 位以及之后的元素向后移动1 位,然后设置value。 要注意的是,insert 时有可能需要数组扩容。 有效元素的个数的含义:对于a 数组,调用一次delete 之后,a 数组的长度不变, 长度依然为5,然而有效元素的个数为4 个。 Tips: insert 方法中,如何判断是否需要扩容:比较有效元素的个数和数组的长度,如果这 两者一致,则需要扩容。 public class javaNineDay { static int[] arr = { 1, 2, 3, 4, 2, 5, 6, 7, 2, 8, 9 }; static int index = arr.length; public static void main(String[] args) { System.out.print("初始数组为:"); for(int term:arr) System.out.print(term+" "); System.out.print("\n删除2后的数组为:"+" "); delete(2); System.out.print("\n插入100后的数组为:"+" "); insert(5, 100); } public static void delete(int pos) { int num = index; for (int i = 0; i < index; i++) { if (arr[i] == pos && i < index - 1) { System.arraycopy(arr, i + 1, arr, i, index - 1 - i); arr[--num] = 0; } else if (arr[i] == pos && i == index - 1) { arr[--num] = 0; } } index = num; for (int term : arr) { if (term > 0) { System.out.print(term + " "); } } } public static void insert(int pos, int value) { int arrays[]; if (index == arr.length) { arrays = new int[index + 1]; System.arraycopy(arr, 0, arrays, 0, index); } else arrays = arr; System.arraycopy(arrays, pos, arrays, pos + 1, index - pos); arrays[pos] = value; index++; for (int term : arrays) { if (term > 0) { System.out.print(term + " "); } } } } 7. *完成数组的冒泡排序算法:给定一个数组:int[] a = {1,3,2,7,5},利用冒泡排序对其按照 从小到大的顺序排序,然后输出结果。 public class javaSevenDayLianXi { public static void main(String[] args) { int[] arr = { 1, 3, 2, 7, 5 }; int term = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { term = arr[j]; arr[j] = arr[i]; arr[i] = term; } } } System.out.print("按冒泡排序后的结果为:"); for (int i : arr) System.out.print(i + " "); } } 8. *使用第二种算法对数组进行排序 import java.util.Arrays; public class javaSevenDayLianXi { public static void main(String[] args) { int[] arr = { 1, 3, 2, 7, 5 }; Arrays.sort(arr); System.out.print("用第二种方法给数组排序后的结果为:"); for (int i : arr) System.out.print(i + " "); } } 杨辉三角的特点: 1. 第i 行有i 个元素 2. 每一行的第一个元素和最后一个元素都为1 3. 除了1 之外,每个元素的值,都等于上一行同位置的元素以及前一个元素的和。 例如: 1 4 6 4 1 的下一行 1 a1 a2 a3 a4 1 a1 = 4 + 1 = 5 a2 = 6 + 4 = 10 a3 = 4 + 6 = 10 a4 = 1 + 4 = 5 依次类推。 要求:读入一个整数n,输出杨辉三角的前n 行 import java.util.Scanner; public class javaSevenDayLianXi { public static void main(String[] args) { Scanner sca = new Scanner(System.in); System.out.print("请输入杨辉三角的行数:"); int row = sca.nextInt(); int arr[][] = getArrays(row); for (int[] a : arr) { for (int term : a) System.out.print((term> 0 ? term : "") + " "); System.out.println(); } } public static int[][] getArrays(int row) { int arr[][] = new int[row][row]; for (int i = 0; i < arr.length; i++) { arr[i][0] = 1; arr[i][i] = 1; } for (int i = 1; i < arr.length; i++) { for (int j = 1; j <= i; j++) { arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1]; } } return arr; } } 9. *数学黑洞6174 已知:一个任意的四位正整数。将数字重新组合成一个最大的数和最小的数相减,重复 这个过程,最多七步, 必得6174。即:7641-1467=6174。将永远出不来。 求证:所有四位数数字(全相同的除外),均能得到6174。输出掉进黑洞的步数。 import java.util.Arrays; import java.util.Scanner; public class javaSixDay { public static void main(String[] args) { Scanner sca = new Scanner(System.in); int shu, max = 0, min = 0, result = 0, biaoShi = 0; int[] arr = new int[4]; while (true) { boolean bool = true; int count = 0; if (biaoShi == 0) System.out.print("请输入一个四位数(全相同的除外):"); shu = sca.nextInt(); if (shu >= 1000 && shu <= 9999) { result = shu; do { arr[0] = result / 1000; arr[1] = result / 100 % 10; arr[2] = result / 10 % 10; arr[3] = result % 10; if (arr[0] == arr[1] && arr[1] == arr[2] && arr[2] == arr[3]) { System.out.print("请重新输入一个四位数:"); bool = false; biaoShi=1; break; } else biaoShi = 0; max = setResult(arr, 0); min = setResult(arr, 1); result = max - min; count++; } while (result != 6174); if (bool) System.out.println(shu + "运行了" + count + "次后,结果变为6174。"); }else biaoShi=0; } } // 计算最大值,最小值 public static int setResult(int[] shu, int biaoshi) { int result = 0, j; Arrays.sort(shu); if (biaoshi == 0) { j = 0; for (int i : shu) { result += (int) (i * Math.pow(10, j));// 获取最大值 j++; } } if (biaoshi == 1) { j = 3; for (int i : shu) { result += (int) (i * Math.pow(10, j));// 获取最小值 j--; } } return result; } } 10. *筛选法求质数:输入一个整数n,求小于这个整数的所有质数。 算法:定义一个长度为n 的boolean 数组,true 表示是质数,false 表示不是质数。初始 均为true。之后从2 开始循环: 1. 找到第一个值为true 的下标i 2. 把所有下标为i 的倍数的值置为false。 直到扫描完数组中的所有数值。 最后遍历数组,如果下标i 的值为true,则说明i 为质数。 import java.util.Scanner; public class javaSevenDayLianXi { public static void main(String[] args) { Scanner sca = new Scanner(System.in); System.out.print("请输入一个大于2的整数:"); int n = sca.nextInt(); int count = 0; boolean[] bool = new boolean[n]; if (true) { bool[2] = true; for (int i = 3; i < n; i++) { if (i % 2 == 0) bool[i] = false; else bool[i] = true; } for (int i = 3; i <= n / 2; i++) { for (int j = i; j < n; j += 2) { if (j % i == 0 && j != i) bool[j] = false; } } System.out.print("小于" + n + "的所有质数为:"); for (boolean lean : bool) { if (lean) System.out.print(count + " "); count++; } } else System.out.println("输入的数不大于2!"); } } 11. **定义一个奇数阶二维数组,把每个元素顺序填入不同的自然数,要求行列和对角线元素 相加的结果相等 算法: 1. 第一个数字填在第一行正中间 2. 如果可以填在斜上方,则数字尽量填在斜上方 3. 如果斜上方出了上边界,则数字填入下一列最下端 4. 如果斜上方出了右边界,则数字填入上一行最左端 5. 如果既出了右边界,又出了上边界,则数字填入上一个数字的下方的 6. 如果斜上方已经被填过,则数字填入上一个数字的下方。 要求:读入一个奇数n,按照上述规则,输出n*n 的方阵。 import java.util.Scanner; public class javaEightDayLianXi { public static void main(String[] args) { Scanner sca = new Scanner(System.in); int num, count = 0; int rowSub = 0, listSub = 0; while (true) { System.out.print("请输入一个奇数:"); num = sca.nextInt(); if (num % 2 != 0) break; } int arr[][] = new int[num][num]; listSub = num / 2; while (count < num * num) { count++; arr[rowSub][listSub] = count; if (rowSub == 0 && listSub < num - 1) { rowSub = num - 1; listSub++; } else if (rowSub > 0 && listSub < num - 1 && arr[rowSub - 1][listSub + 1] == 0) { rowSub--; listSub++; } else if (rowSub > 0 && listSub == num - 1) { rowSub--; listSub = 0; } else if (rowSub > 0 && listSub < num - 1 && arr[rowSub - 1][listSub + 1] != 0) { rowSub++; } else if (rowSub == 0 && listSub == num - 1) { rowSub++; } } for (int[] score : arr) { for (int term : score) System.out.print(term + "\t"); System.out.println(""); System.out.println(""); } } } 12. **十五个猴子围成一圈选大王,依次1-7 循环报数,报到7 的猴子被淘汰,直到最后一 只猴子成为大王。问,哪只猴子最后能成为大王? 总数 要输入 ,从哪一只开始要输入,点到几要输入 public class javaSevenDay { public static void main(String[] args) { int all = 15, start = 1, end = 7, num; System.out.print("出局的猴子有:"); int[] monkey = new int[all + 1]; num = all + 1; monkey[0] = 0; for (int i = 1; i < monkey.length; i++) monkey[i] = 1; for (int i = 1; i <= end; i++) { if (all == 1) break; else if (i == end) { all--; i = 0; monkey[start] = 0; System.out.print(start + " "); } do { start++; start = start % num; } while (monkey[start] != 1); } System.out.println(); System.out.print("猴王为第" + start + "只猴子。"); } } 13. **螺旋填数 读入两个整数m,n,输出一个m 行n 列的矩阵,这个矩阵是1~m*n 这些自然数按照右、 下、左、上螺旋填入的结果。 例如:读入4, 5, 则输出 1 2 3 4 5 14 15 16 17 6 13 20 19 18 7 12 11 10 9 8 import java.util.Scanner; public class javaEightDay { public static void main(String[] args) { Scanner sca = new Scanner(System.in); System.out.print("请输入矩阵的行数"); int row = sca.nextInt(); System.out.print("\n请输入矩阵的列数:"); int list = sca.nextInt(); int[][] arr = new int[row][list]; int count = 0; //数组存储的值。 int rowSub = 0, listSub = 0;//数组的行下标和列下标 final int up = 1, down = -1, left = 2, right = -2;//螺旋时的四个状态 int start = right;//初始状态 int circle = 1; //第一圈 while (count < row * list) { count++; arr[rowSub][listSub] = count; switch (start) { case right: if (listSub < list - circle) listSub++; else { start = down; rowSub++; } break; case down: if (rowSub < row - circle) rowSub++; else { start = left; listSub--; } break; case left: if (listSub > circle - 1) listSub--; else { start = up; rowSub--; } break; case up: if (rowSub > circle) rowSub--; else { circle++; start = right; listSub++; } break; } } for (int[] score : arr) { //输出数组 for (int term : score) System.out.print(term + " "); System.out.println(); } } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 考试专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服