当前位置:   article > 正文

48天笔试训练错题——day31

48天笔试训练错题——day31

目录

选择题

1.

2.

3.

4.

5.

6.

7.

8.

编程题

1. 美国节日

2. 分解因数


选择题

1.

2.

自动变量是线程私有的,不存在线程共享,所有不会存在线程安全问题。

3.

4.

5.

访问数据速度:CPU 缓存 > 内存 > 硬盘

6.

可以把分页是虚拟管理系统看作是一本书,查看页面就是去书中对应章节找资料。

缺页中断也就可以看作是从一本书中没有想要的资料,得去另一本书中找。

因为书的内容本身是确定的,一张书页的大小与是否引用了想要资料无关。

7.

一个父进程可以创建多个子进程,但是一个子进程只能从属于一个父进程。

子进程退出,父进程还可以继续执行。父子进程之间没有影响。

父进程退出,还未执行完的子进程会被交给 init 进程,此时子进程的父进程就是 init 进程,init 进程没有父进程。

8.

进程是资源分配的基本单位,线程是 cpu 调度的基本单位。

编程题

1. 美国节日

代码实现:

  1. import java.util.*;
  2. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner in = new Scanner(System.in);
  6. // 注意 hasNext 和 hasNextLine 的区别
  7. while (in.hasNextInt()) { // 注意 while 处理多个 case
  8. int n = in.nextInt();
  9. print(n);
  10. }
  11. }
  12. public static void print(int y) {
  13. // 第一个节日
  14. System.out.println(y + "-01-01");
  15. // 找到 1 月 1 日是周几,然后根据公式计算
  16. // 1月的第三个星期一
  17. int w = getWeekDay(y, 1, 1);
  18. int d = getDays(3, w, 1);
  19. System.out.printf("%04d-%02d-%02d\n", y, 1, d);
  20. // 2月的第三个星期一
  21. w = getWeekDay(y, 2, 1);
  22. d = getDays(3, w, 1);
  23. System.out.printf("%04d-%02d-%02d\n", y, 2, d);
  24. // 5月的最后一个星期一:阵亡将士纪念日
  25. // 将 6 月 1 日看作是 5 月 32 号,
  26. w = getWeekDay(y, 6, 1);
  27. int t = ((w == 1) ? 7 : (w - 1));
  28. d = 32 - t;
  29. System.out.printf("%04d-%02d-%02d\n", y, 5, d);
  30. // 7月4日:美国国庆
  31. System.out.printf("%04d-%02d-%02d\n", y, 7, 4);
  32. // 9月的第一个星期一
  33. w = getWeekDay(y, 9, 1);
  34. d = getDays(1, w, 1);
  35. System.out.printf("%04d-%02d-%02d\n", y, 9, d);
  36. // 11月的第四个星期四:感恩节
  37. w = getWeekDay(y, 11, 1);
  38. d = getDays(4, w, 4);
  39. System.out.printf("%04d-%02d-%02d\n", y, 11, d);
  40. // 12月25日:圣诞节
  41. System.out.printf("%04d-%02d-%02d\n", y, 12, 25);
  42. System.out.println();
  43. }
  44. // 获取第 n 个星期的星期 e 是几号
  45. // n: 找第几个星期
  46. // w: 1 号是星期几
  47. // e: 找星期几
  48. public static int getDays(int n, int w, int e) {
  49. return 1 + (n - 1) * 7 + (7 - w + e) % 7;
  50. }
  51. // 以公元前 1 年,12 月 31 日(当天星期七)为基准,获取 y 年 m 月 d 日 是星期几
  52. public static int getWeekDay(int y, int m, int d) {
  53. // 总天数 = 经历的整年天数 + 经过的闰年天数 + 最后一年经历的天数
  54. // = 365 *(n - 1) + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + 最后一年经历的天数
  55. int days = (y - 1) + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400;
  56. // 最后一年经历的天数 = 经过的整月天数 + 最后一个月的天数(判断是否存在闰年情况)
  57. int[] hash = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  58. for (int i = 1; i < m; i++) {
  59. days += hash[i];
  60. }
  61. if (isLeapYear(y) && m > 2) {
  62. days++;
  63. }
  64. days += d;
  65. return days % 7 == 0 ? 7 : days % 7;
  66. }
  67. // 判断闰年
  68. public static boolean isLeapYear(int y) {
  69. return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0);
  70. }
  71. }

额,虽然提示超时了,但是我代码自测没问题。

2. 分解因数

就是碰到约数就一直除,直到没有约数为止。

代码实现:

  1. import java.util.*;
  2. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner in = new Scanner(System.in);
  6. // 注意 hasNext 和 hasNextLine 的区别
  7. while (in.hasNextInt()) { // 注意 while 处理多个 case
  8. int n = in.nextInt();
  9. print(n);
  10. }
  11. }
  12. public static void print(int n) {
  13. // 先判断是否是素数,如果是,直接输出即可。
  14. if (isPrime(n)) {
  15. System.out.println(n + " = " + n);
  16. return;
  17. }
  18. int t = n;
  19. // arr 是用来存放因子的
  20. ArrayList<Integer> arr = new ArrayList<>();
  21. for (int i = 2 ; i <= Math.sqrt(n); i++) {
  22. if (n % i == 0) {
  23. while (n % i == 0) {
  24. n /= i;
  25. arr.add(i);
  26. }
  27. }
  28. }
  29. // 如果 n 不为 1,说明最后 n 已经是素数了,素数也是因子之一
  30. if (n != 1) {
  31. arr.add(n);
  32. }
  33. // 按格式输出
  34. System.out.print(t + " = ");
  35. for (int i = 0; i < arr.size(); i++) {
  36. int x = arr.get(i);
  37. if (i == 0) {
  38. System.out.print(x + " ");
  39. } else {
  40. System.out.print("* " + x + " ");
  41. }
  42. }
  43. System.out.println();
  44. }
  45. public static boolean isPrime(int n) {
  46. for (int i = 2 ; i <= Math.sqrt(n); i++) {
  47. if (n % i == 0) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/864441
推荐阅读
相关标签
  

闽ICP备14008679号