云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > java经典基础练习题及参考答案

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

  • 62 次阅读
  • 3 次下载
  • 2025/5/3 9:43:49

}

}

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. 如果斜上方出了右边界,则数字填入上一行最左端

搜索更多关于: java经典基础练习题及参考答案 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

} } 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(\用第二种方法给数组排

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com