最新JAVA经典算法50题,java多线程面试题目

最后

面试题文档来啦,内容很多,485页!

由于笔记的内容太多,没办法全部展示出来,下面只截取部分内容展示。

1111道Java工程师必问面试题

MyBatis 27题 + ZooKeeper 25题 + Dubbo 30题:

Elasticsearch 24 题 +Memcached + Redis 40题:

Spring 26 题+ 微服务 27题+ Linux 45题:

Java面试题合集:

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  1. public static void main(String[] args) {

  2. for (int i = 1; i <= 9; i++) {

  3. for (int j = 1; j <= 9; j++)

  4. System.out.print(i + “*” + j + “=” + (i*j) + “\t”);

  5. System.out.println();

  6. }

  7. }

  8. }

不现重复的乘积(下三角)

  1. public class Demo16 {

  2. public static void main(String[] args) {

  3. for (int i = 1; i <= 9; i++) {

  4. for (int j = 1; j <= i; j++)

  5. System.out.print(i + “*” + j + “=” + (i*j) + “\t”);

  6. System.out.println();

  7. }

  8. }

  9. }

【程序17】   题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩

下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

1.程序分析:采取逆向思维的方法,从后往前推断。

  1. public class Demo17 {

  2. public static void main(String[] args) {

  3. int sum = 1;

  4. for (int i = 0; i < 9; i++) {

  5. sum = (sum + 1) * 2;

  6. }

  7. System.out.println(“第一天共摘”+sum);

  8. }

  9. }

【程序18】   题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程

序找出三队赛手的名单。

  1. public class Demo18 {

  2. static char[] m = { ‘a’, ‘b’, ‘c’ };

  3. static char[] n = { ‘x’, ‘y’, ‘z’ };

  4. public static void main(String[] args) {

  5. for (int i = 0; i < m.length; i++) {

  6. for (int j = 0; j < n.length; j++) {

  7. if (m[i] == ‘a’ && n[j] == ‘x’) {

  8. continue;

  9. } else if (m[i] == ‘a’ && n[j] == ‘y’) {

  10. continue;

  11. } else if ((m[i] == ‘c’ && n[j] == ‘x’)

  12. || (m[i] == ‘c’ && n[j] == ‘z’)) {

  13. continue;

  14. } else if ((m[i] == ‘b’ && n[j] == ‘z’)

  15. || (m[i] == ‘b’ && n[j] == ‘y’)) {

  16. continue;

  17. } else

  18. System.out.println(m[i] + " vs " + n[j]);

  19. }

  20. }

  21. }

  22. }

  1. public class Demo18 {

  2. public String a, b, c;

  3. public Demo18(String a, String b, String c) {

  4. this.a = a;

  5. this.b = b;

  6. this.c = c;

  7. }

  8. public static void main(String[] args) {

  9. Demo18 arr_a = new Demo18(“a”, “b”, “c”);

  10. String[] b = { “x”, “y”, “z” };

  11. for (int i = 0; i < 3; i++) {

  12. for (int j = 0; j < 3; j++) {

  13. for (int k = 0; k < 3; k++) {

  14. Demo18 arr_b = new Demo18(b[i], b[j], b[k]);

  15. if (!arr_b.a.equals(arr_b.b) & !arr_b.b.equals(arr_b.c)

  16. & !arr_b.c.equals(arr_b.a) & !arr_b.a.equals(“x”)

  17. & !arr_b.c.equals(“x”) & !arr_b.c.equals(“z”)) {

  18. System.out.println(arr_a.a + “–” + arr_b.a);

  19. System.out.println(arr_a.b + “–” + arr_b.b);

  20. System.out.println(arr_a.c + “–” + arr_b.c);

  21. }

  22. }

  23. }

  24. }

  25. }

  26. }

【程序19】  题目:打印出如下图案(菱形)

1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重for循环,第一层控制行,第二层控制列。

三角形:

*

***

******

********

******

***

*

  1. public class Demo19 {

  2. public static void main(String[] args) {

  3. int i=0;

  4. int j=0;

  5. for ( i = 1; i <= 4; i++) {

  6. for ( j = 1; j <= 2 * i - 1; j++)

  7. System.out.print(“*”);

  8. System.out.println();

  9. }

  10. for ( i = 3; i >= 1; i–) {

  11. for ( j = 1; j <= 2 * i - 1; j++)

  12. System.out.print(“*”);

  13. System.out.println();

  14. }

  15. }

  16. }

菱形:

*

***

*****

*******

*****

***

*

  1. public class Demo19 {

  2. public static void main(String[] args) {

  3. int i = 0;

  4. int j = 0;

  5. for (i = 1; i <= 4; i++) {

  6. for (int k = 1; k <= 4 - i; k++)

  7. System.out.print( " " );

  8. for (j = 1; j <= 2 * i - 1; j++)

  9. System.out.print(“*”);

  10. System.out.println();

  11. }

  12. for (i = 3; i >= 1; i–) {

  13. for (int k = 1; k <= 4 - i; k++)

  14. System.out.print( " " );

  15. for (j = 1; j <= 2 * i - 1; j++)

  16. System.out.print(“*”);

  17. System.out.println();

  18. }

  19. }

  20. }

【程序20】   题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。

1.程序分析:请抓住分子与分母的变化规律。

  1. public class Demo20 {

  2. public static void main(String[] args) {

  3. float fm = 1.0f;

  4. float fz = 1.0f;

  5. float temp;

  6. float sum = 0f;

  7. for (int i = 0; i < 20; i++) {

  8. temp = fm;

  9. fm = fz;

  10. fz = fz + temp;

  11. System.out.println((int) fz + “/” + (int) fm);

  12. sum += fz / fm;

  13. }

  14. System.out.println(sum);

  15. }

  16. }

【程序21】   题目:求1+2!+3!+…+20!的和。

1.程序分析:此程序只是把累加变成了累乘。

  1. public class Demo21 {

  2. public static void main(String[] args) {

  3. long sum = 0;

  4. long fac = 1;

  5. for (int i = 1; i <= 20; i++) {

  6. fac = fac * i;

  7. sum += fac;

  8. }

  9. System.out.println(sum);

  10. }

  11. }

【程序22】   题目:利用递归方法求5!。

1.程序分析:递归公式:f(n)=f(n-1)*4!

  1. import java.util.Scanner;

  2. public class Demo22 {

  3. public static long fac(int n) {

  4. long value = 0;

  5. if (n == 1 || n == 0) {

  6. value = 1;

  7. } else if (n > 1) {

  8. value = n * fac(n - 1);

  9. }

  10. return value;

  11. }

  12. public static void main(String[] args) {

  13. System.out.println(“请输入一个数:”);

  14. Scanner in = new Scanner(System.in);

  15. int n = in.nextInt();

  16. System.out.println(n + “的阶乘为:” + fac(n));

  17. }

  18. }

【程序23】   题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两

岁。最后问第一个人,他说是10岁。请问第五个人多大?

1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。

直接求解:

  1. public class Demo23 {

  2. public static void main(String[] args) {

  3. int n = 10;

  4. for (int i = 0; i < 4; i++) {

  5. n = n + 2;

  6. }

  7. System.out.println(“第五个人” + n + “岁”);

  8. }

  9. }

递归求解:

  1. public class Demo23 {

  2. public static int getAge(int n) {

  3. if (n == 1) {

  4. return 10;

  5. }

  6. return 2 + getAge(n - 1);

  7. }

  8. public static void main(String[] args) {

  9. System.out.println(“第五个的年龄为” + getAge(5));

  10. }

  11. }

【程序24】   题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

本题原方法:

  1. import java.util.Scanner;

  2. public class Demo24 {

  3. public static void main(String[] args) {

  4. Demo24 use = new Demo24();

  5. System.out.println(“请输入:”);

  6. Scanner in = new Scanner(System.in);

  7. long a = in.nextLong();

  8. if (a < 0 || a >= 100000) {

  9. System.out.println(“Error Input, please run this program Again!”);

  10. System.exit(0);

  11. }

  12. if (a >= 0 && a <= 9) {

  13. System.out.println(a + “是一位数”);

  14. System.out.println(“按逆序输出是:” + a);

  15. } else if (a >= 10 && a <= 99) {

  16. System.out.println(a + “是二位数”);

  17. System.out.println(“按逆序输出是:”);

  18. use.converse(a);

  19. } else if (a >= 100 && a <= 999) {

  20. System.out.println(a + “是三位数”);

  21. System.out.println(“按逆序输出是:”);

  22. use.converse(a);

  23. } else if (a >= 1000 && a <= 9999) {

  24. System.out.println(a + “是四位数”);

  25. System.out.println(“按逆序输出是:”);

  26. use.converse(a);

  27. } else if (a >= 10000 && a <= 99999) {

  28. System.out.println(a + “是五位数”);

  29. System.out.println(“按逆序输出是:”);

  30. use.converse(a);

  31. }

  32. }

  33. public void converse(long l) {

  34. String s = Long.toString(l);

  35. char[] ch = s.toCharArray();

  36. for (int i = ch.length - 1; i >= 0; i–) {

  37. System.out.print(ch[i]);

  38. }

  39. }

  40. }

个人版方法:

  1. import java.util.Scanner;

  2. public class Demo24 {

  3. public static void main(String[] args) {

  4. System.out.println(“请输入:”);

  5. Scanner in = new Scanner(System.in);

  6. String str = in.next();

  7. if (str.matches(“\\d+”)) { //正则表达式

  8. System.out.println(“输入的是” + str.length() + “位数”);

  9. StringBuffer buf = new StringBuffer(str);

  10. System.out.println(buf.reverse());//字符串反转

  11. }

  12. }

  13. }

【程序25】   题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

原方法:

  1. import java.util.Scanner;

  2. public class Demo25 {

  3. static int[] a = new int[5];

  4. static int[] b = new int[5];

  5. public static void main(String[] args) {

  6. boolean is = false;

  7. System.out.println(“Please input:”);

  8. Scanner in = new Scanner(System.in);

  9. long l = in.nextLong();

  10. if (l > 99999 || l < 10000) {

  11. System.out.println(" Input error, please input again!");

  12. l = in.nextLong();

  13. }

  14. for ( int i = 4; i >= 0; i–) {

  15. a[i] = (int) (l / (long) Math.pow(10, i));

  16. l = (l % (long) Math.pow(10, i));

  17. }

  18. System.out.println();

  19. for (int i = 0, j = 0; i < 5; i++, j++) {

  20. b[j] = a[i];

  21. }

  22. for (int i = 0, j = 4; i < 5; i++, j–) {

  23. if (a[i] != b[j]) {

  24. is = false;

  25. break;

  26. } else {

  27. is = true;

  28. }

  29. }

  30. if (is == false) {

  31. System.out.println(“is not a Palindrom!”);

  32. } else if (is == true) {

  33. System.out.println(“is a Palindrom!”);

  34. }

  35. }

  36. }

个人版:

  1. import java.util.Scanner;

  2. public class Demo25 {

  3. public static void main(String[] args) {

  4. System.out.println(“请输入:”);

  5. Scanner in = new Scanner(System.in);

  6. String str = in.next();

  7. int l = Integer.parseInt(str);//转换成整数

  8. if (l < 10000 || l > 99999) {

  9. System.out.println(“输入错误!”);

  10. System.exit(0);

  11. }

  12. boolean is=false;

  13. char[] ch = str.toCharArray();

  14. for(int i=0;i <ch.length/2;i++){

  15. if(ch[i]!=ch[ch.length-i-1]){

  16. is=false;

  17. }else{

  18. is=true;

  19. }

  20. }

  21. if(is){

  22. System.out.println(“这是一个回文!”);

  23. }else{

  24. System.out.println(“不是一个回文!”);

  25. }

  26. }

  27. }

【程序26】   题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。

  1. import java.util.Scanner;

  2. public class Demo26 {

  3. public static void main(String[] args) {

  4. char weekSecond;//保存第二字母

  5. Scanner in = new Scanner(System.in);//接收用户输入

  6. System.out.println(“请输入星期的第一个字母:”);

  7. String letter = in.next();

  8. if (letter.length() == 1) {//判断用户控制台输入字符串长度是否是一个字母

  9. char weekFirst = letter.charAt(0);//取第一个字符

  10. switch (weekFirst) {

  11. case ‘m’:

  12. case ‘M’:

  13. System.out.println(“星期一(Monday)”);

  14. break;

  15. case ‘t’:

  16. case ‘T’:

  17. System.out.print(“由于星期二(Tuesday)与星期四(Thursday)均以字母T开头,故需输入第二个字母才能正确判断:”);

  18. letter = in.next();

  19. if (letter.length() == 1) {

  20. weekSecond = letter.charAt(0);

  21. if (weekSecond == ‘U’ || weekSecond == ‘u’) {

  22. System.out.println(“星期二(Tuesday)”);

  23. break;

  24. } else if (weekSecond == ‘H’ || weekSecond == ‘h’) {

  25. System.out.println(“星期四(Thursday)”);

  26. break;

  27. } else {

  28. System.out.println(“Error!”);

  29. break;

  30. }

  31. } else {

  32. System.out.println(“输入错误,只能输入一个字母,程序结束!”);

  33. break;

  34. }

  35. case ‘w’:

  36. case ‘W’:

  37. System.out.println(“星期三(Wednesday)”);

  38. break;

  39. case ‘f’:

  40. case ‘F’:

  41. System.out.println(“星期五(Friday)”);

  42. break;

  43. case ‘s’:

  44. case ‘S’:

  45. System.out.print(“由于星期六(Saturday)与星期日(Sunday)均以字母S开头,故需输入第二个字母才能正确判断:”);

  46. letter = in.next();

  47. if (letter.length() == 1) {

  48. weekSecond = letter.charAt(0);

  49. if (weekSecond == ‘A’ || weekSecond == ‘a’) {

  50. System.out.println(“星期六(Saturday)”);

  51. break;

  52. } else if (weekSecond == ‘U’ || weekSecond == ‘u’) {

  53. System.out.println(“星期日(Sunday)”);

  54. break;

  55. } else {

  56. System.out.println(“Error!”);

  57. break;

  58. }

  59. } else {

  60. System.out.println(“输入错误,只能输入一个字母,程序结束!”);

  61. break;

  62. }

  63. default:

  64. System.out.println(“输入错误,不能识别的星期值第一个字母,程序结束!”);

  65. break;

  66. }

  67. } else {

  68. System.out.println(“输入错误,只能输入一个字母,程序结束!”);

  69. }

  70. }

  71. }

【程序27】   题目:求100之内的素数

  1. public class Demo27 {

  2. public static void main(String args[]) {

  3. int sum, i;

  4. for (sum = 2; sum <= 100; sum++) {

  5. for (i = 2; i <= sum / 2; i++) {

  6. if (sum % i == 0)

  7. break;

  8. }

  9. if (i > sum / 2)

  10. System.out.println(sum + “是素数”);

  11. }

  12. }

  13. }

  1. public class Demo27{

  2. public static void main(String args[]){

  3. int w=1;

  4. for(int i=2;i <=100;i++){

  5. for(int j=2;j<i;j++){

  6. w=i%j;

  7. if(w==0)break;

  8. }

  9. if(w!=0)

  10. System.out.println(i+“是素数”);

  11. }

  12. }

  13. }

【程序28】   题目:对10个数进行排序。

1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换。

本例代码为生成随机10个数排序,并输入1个数,插入重排序输出:

  1. import java.util.Arrays;

  2. import java.util.Random;

  3. import java.util.Scanner;

  4. public class Demo28 {

  5. public static void main(String[] args) {

  6. int arr[] = new int[11];

  7. Random r = new Random();

  8. for (int i = 0; i < 10; i++) {

  9. arr[i] = r.nextInt(100) + 1; //得到10个100以内的整数

  10. }

  11. Arrays.sort(arr);

  12. for (int i = 0; i < arr.length; i++) {

  13. System.out.print(arr[i] +“\t”);

  14. }

  15. System.out.print(“\nPlease Input a int number:” );

  16. Scanner in = new Scanner(System.in);

  17. arr[10] = in.nextInt();

  18. Arrays.sort(arr);

  19. for (int i = 0; i < arr.length; i++) {

  20. System.out.print(arr[i] +“\t”);

  21. }

  22. }

  23. }

个人代码:

  1. import java.util.Arrays;

  2. import java.util.Scanner;

  3. public class Demo28 {

  4. public static void main(String[] args) {

  5. System.out.println(“请输入10个数:”);

  6. Scanner in = new Scanner(System.in);

  7. int[] arr = new int[10];

  8. for (int i = 0; i < 10; i++) {

  9. arr[i] = in.nextInt();

  10. }

  11. System.out.println(“原数组为:”);

  12. for (int x : arr) {//foreach遍历

  13. System.out.print( x + “\t”);

  14. }

  15. Arrays.sort(arr);

  16. System.out.println();

  17. System.out.println(“排序后为:”);

  18. for (int i = 0; i < arr.length; i++) {

  19. System.out.print(arr[i] + “\t”);

  20. }

  21. }

  22. }

【程序29】   题目:求一个3*3矩阵主对角线元素之和。

1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。

public class Demo29 {

public static void main(String[] args) {

double sum = 0;

int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 7, 8 } };

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++) {

if (i == j)

sum = sum + array[i][j];

}

System.out.println(sum);

}

}

主负对角线:

for(i=0;i<n;i++)

for(j=0;j<n;j++)

{

if(i==j) sum1+=a[i][j];

if(i+j==n-1) sum2+=a[i][j];

}

【程序30】   题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

1.程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。

import java.util.Random;

public class Demo30 {

public static void main(String[] args) {

int temp = 0;

int arr[] = new int[12];

Random r = new Random();

for (int i = 0; i <= 10; i++)

arr[i] = r.nextInt(1000);

for (int i = 0; i <= 10; i++)

System.out.print(arr[i] + “\t”);

for (int i = 0; i <= 9; i++)

for (int k = i + 1; k <= 10; k++)

if (arr[i] > arr[k]) {

temp = arr[i];

arr[i] = arr[k];

arr[k] = temp;

}

System.out.println();

for (int k = 0; k <= 10; k++)

System.out.print(arr[k] + “\t”);

arr[11] = r.nextInt(1000);

for (int k = 0; k <= 10; k++)

if (arr[k] > arr[11]) {

temp = arr[11];

for (int j = 11; j >= k + 1; j–)

arr[j] = arr[j - 1];

arr[k] = temp;

}

System.out.println();

for (int k = 0; k <= 11; k++)

System.out.print(arr[k] + “\t”);

}

}

【程序31】   题目:将一个数组逆序输出。

程序分析:用第一个与最后一个交换。

用逆序循环控制变量输出:

public class Demo31 {

public static void main(String[] args) {

int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

for (int i = a.length - 1; i >= 0; i–) {

System.out.print(a[i] + " ");

}

}

}

【程序32】   题目:取一个整数a从右端开始的第4~7位数字。

import java.util.*;

public class Demo32 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print(“请输入一个7位以上的正整数:”);

long l = in.nextLong();

String str = Long.toString(l);

char[] ch = str.toCharArray();

int j=ch.length;

if (j<7){System.out.println(“输入错误!”);

} else {

System.out.println(“截取从右端开始的4~7位是:”+ch[j-7]+ch[j-6]+ch[j-5]+ch[j-4]);

}

}

}

import java.util.Scanner;

public class Demo32{

public static void main(String[] args) {

int a = 0;

Scanner s = new Scanner(System.in);

long b = s.nextLong();

a = (int) (b % 10000000 / 1000);

System.out.println(a);

}

}

【程序33】   题目:打印出杨辉三角形(要求打印出10行如下图)

1.程序分析:

1

1   1

1   2   1

1   3   3   1

1   4   6   4   1

1   5   10   10   5   1

public class Demo33 {

public static void main(String args[]) {

int i, j;

int a[][];

int n = 10;

a = new int[n][n];

for (i = 0; i < n; i++) {

a[i][i] = 1;

a[i][0] = 1;

}

for (i = 2; i < n; i++) {

for (j = 1; j <= i - 1; j++) {

a[i][j] = a[i - 1][j - 1] + a[i - 1][j];

}

}

for (i = 0; i < n; i++) {

for (j = 0; j <= i; j++) {

System.out.printf(a[i][j] + “\t”);

}

System.out.println();

}

}

}

【程序34】   题目:输入3个数a,b,c,按大小顺序输出。

(也可互相比较交换排序)

import java.util.Arrays;

public class Demo34 {

public static void main(String[] args) {

int[] arrays = { 800, 56, 500 };

Arrays.sort(arrays);

for (int n = 0; n < arrays.length; n++)

System.out.println(arrays[n]);

}

}

if(x > y) { int t = x; x = y; y = t; } if(x > z) { int t = x; x = z; z = t; } if(y > z) { int t = y; y = z; z = t; }

【程序35】   题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

import java.util.*;

public class Demo35 {

public static void main(String[] args) {

int i, min=0, max=0, n, temp1, temp2;

int a[];

System.out.println(“定义数组的长度:”);

Scanner in = new Scanner(System.in);

n = in.nextInt();

a = new int[n];

for (i = 0; i < n; i++) {

System.out.print(“输入第” + (i + 1) + “个数据:”);

a[i] = in.nextInt();

}

for (i = 1; i < n; i++) {

if (a[i] > a[max])

max = i;

if (a[i] < a[min])

min = i;

}

temp1 = a[0];

a[0] = a[max];

a[max] = temp1;

temp2 = a[min];

if (min != 0) { // 如果最小值不是a[0],执行下面

a[min] = a[n - 1];

a[n - 1] = temp2;

} else {  //如果最小值是a[0],执行下面

a[max] = a[n - 1];

a[n - 1] = temp1;

}

for (i = 0; i < n; i++) {

System.out.print(a[i] + " " );

}

}

}

【程序36】   题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

import java.util.LinkedList;

import java.util.List;

import java.util.Scanner;

public class Demo36 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println(“输入数字个数n:”);

int n = in.nextInt();

System.out.println(“输入后移位数m:”);

int m = in.nextInt();

LinkedList list = new LinkedList();

for (int i = 0; i < n; i++) {

System.out.println(“请输入第”+(i+1)+“个数:”);

list.add(in.nextInt());

}

System.out.println(“原数据排序为:”);

for (int t : list) {

System.out.print(t + " " );

}

System.out.println();

List temp1 = list.subList(list.size() - m, list.size());

List temp2 = list.subList(0, list.size() - m);

temp2.addAll(0, temp1);

System.out.println(“移动后排序为;”);

for (int t : temp2) {

System.out.print(t + " " );

}

}

}

import java.util.*;

public class Demo36{

public static void main(String[] args){

Scanner in=new Scanner(System.in);

System.out.println(“请定义数组的长度:”);

int n=in.nextInt();

System.out.println(“请输入移动的位数:”);

int m=in.nextInt();

int [] arr=new int [n];

int [] brr=new int [n];

for(int i=0;i<n;i++){

System.out.println(“请输入第”+(i+1)+“个数:”);

arr[i]=in.nextInt();

}

System.out.println(“排序前:”);

for(int i=0;i<n;i++){

System.out.print(arr[i]+" ");

}

System.out.println();

for(int i=0;i<m;i++){

brr[i]=arr[n-m+i];

}

for(int i=0;i<n-m;i++){

arr[m+i]=arr[i];

}

for(int i=0;i<m;i++){

arr[i]=brr[i];

}

System.out.println(“排序后:”);

for(int i=0;i<n;i++){

System.out.print(arr[i]+" ");

}

}

}

【程序37】   题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

(约瑟夫环问题,百度百科有时间复杂度最简单的数学方法)

原例代码:

import java.util.Scanner;

public class Demo37 {

public static void main(String[] args) {

System.out.println(“请输人数n:”);

Scanner in = new Scanner(System.in);

int n = in.nextInt();

boolean[] arr = new boolean[n];

for (int i = 0; i < arr.length; i++) {

arr[i] = true; //下标为TRUE时说明还在圈里

}

int leftCount = n;

int countNum = 0;

int index = 0;

while (leftCount > 1) {

if (arr[index] == true) { //当在圈里时

countNum++;  //报数递加

if (countNum == 3) { //报数为3时

countNum = 0; //从零开始继续报数

arr[index] = false; //此人退出圈子

leftCount–; //剩余人数减一

}

}

index++; //每报一次数,下标加一

if (index == n) { //是循环数数,当下标大于n时,说明已经数了一圈,

index = 0; //将下标设为零重新开始。

}

}

for (int i = 0; i < n; i++) {

if (arr[i] == true) {

System.out.println(i);

}

}

}

}

个人代码1:

import java.util.Scanner;

public class Demo37 {

public static void main(String[] args) {

System.out.println(“请输入人数:”);

Scanner in = new Scanner(System.in);

int[] a = new int[in.nextInt()];

for (int i = 0; i < a.length; i++) {

a[i] = 1;

}

int left = a.length;

int j = 0;

int num = 0;

while (left > 1) {

if (a[j] == 1) {

num++;

}

if (num == 3) {

a[j] = 0;

num = 0;

left–;

}

j++;

if (j == a.length) {

j = 0;

}

}

for (int i = 0; i < a.length; i++) {

if (a[i] == 1) {

System.out.println(“最后留下的人是”+ (i + 1) + “号”);

break;

}

}

}

}

个人代码2:

import java.util.LinkedList;

import java.util.Scanner;

public class Demo37 {

public static void main(String[] args) {

LinkedList l = new LinkedList();

System.out.println(“请输入人数:”);

Scanner in = new Scanner(System.in);

int len = in.nextInt();

for (int i = 0; i < len; i++) {

l.add(i + 1);

}

int sum = 0;

int temp = 0;

for (int i = 0; sum != len - 1;) {

if (l.get(i) != 0) {

temp++;

}

if (temp == 3) {

l.remove(i);

l.add(i, 0);

temp = 0;

sum++;

}

i++;

if (i == l.size()) {

i = 0;

}

}

for (int t : l) {

if (t != 0) {

System.out.println(“最后留下的人是” + t + “号”);

}

}

}

}

【程序38】   题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

import java.util.Scanner;

public class Demo38 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println(“请输入一个字符串:”);

String mys = in.next();

System.out.println(str_len(mys));

}

public static int str_len(String x) {

return x.length();

}

}

import java.util.Scanner;

public class Demo38 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println(“请输入一个字符串:”);

String mys = in.next();

System.out.println(mys.length());

}

}

【程序39】  题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+…+1/n,当输入n为奇数时,调用函数1/1+1/3+…+1/n

import java.util.Scanner;

public class Demo39 {

public static double ouShu(int n) {

double result = 0;

for (int i = 2; i <= n; i = i + 2) {

result +=  1 / (double) i;

}

return result;

}

public static double jiShu(int n) {

double result = 0;

for (int i = 1; i <= n; i = i + 2) {

result += 1 / (double) i;

}

return result;

}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println(“输入n的值:”);

int n = in.nextInt();

if (n % 2 == 0) { //偶数,1/2+1/4+…+1/n

System.out.println(ouShu(n));

} else { //奇数,1/1+1/3+…+1/n

System.out.println(jiShu(n));

}

}

}

【程序40】  题目:字符串排序。

(利用容器类中的sort方法)

import java.util.*;

public class Demo40 {

public static void main(String[] args) {

ArrayList list = new ArrayList();

list.add(“010102”);

list.add(“010003”);

list.add(“010201”);

Collections.sort(list);

for (int i = 0; i < list.size(); i++) {

System.out.println(list.get(i));

}

}

}

import java.util.*;

public class Demo40 {

public static void main(String[] args){

Scanner in=new Scanner(System.in);

System.out.println(“请定义字符串的个数:”);

int n=in.nextInt();

String[] str=new String[n];

for(int i=0;i<str.length;i++){

System.out.println(“请输入第”+(i+1)+“字符串:”);

str[i]=in.next();

}

strSort(n,str);

System.out.println(“字符串排序后:”);

for(int i=0;i<str.length;i++){

System.out.print(str[i]+" ");

}

}

public static void strSort(int n,String[] arr){

for(int i=0; i<n; i++) {

for(int j=i+1; j<n; j++) {

if(compare(arr[i], arr[j]) == false) {

String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;

}

}

}

}

static boolean compare(String s1, String s2) {

boolean result = true;

for(int i=0; i<s1.length() && i<s2.length(); i++) {

if(s1.charAt(i) > s2.charAt(i)) {

result = false;

break;

} else if(s1.charAt(i) <s2.charAt(i)) {

result = true;

break;

} else {

if(s1.length() < s2.length()) {

result = true;

} else {

result = false;

}

}

}

return result;

}

}

【程序41】   题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

本题源码:

public class Demo41 {

static int ts = 0;// 桃子总数

int fs = 1;// 记录分的次数

static int hs = 5;// 猴子数

int tsscope = 5000;// 桃子数的取值范围,太大容易溢出。

public int fT(int t) {

if (t == tsscope) {

// 当桃子数到了最大的取值范围时取消递归

System.out.println(“结束”);

return 0;

} else {

if ((t - 1) % hs == 0 && fs <= hs) {

if (fs == hs) {

System.out.println(“桃子数=” + ts + “时满足分桃条件”);

}

fs += 1;

return fT((t - 1) / 5 * 4);// 返回猴子拿走一份后的剩下的总数

} else {

// 没满足条件

fs = 1;// 分的次数重置为1

return fT(ts += 1);// 桃子数加+1

}

}

}

public static void main(String[] args) {

new Demo41().fT(0);

}

}

个人修改:

public class Demo41 {

public static void main(String[] args) {

int sum = 0;

for (int i = 6;; i++) {// 最少6个分最后一次

sum = i;// 桃子数

for (int j = 0; j < 5; j++) {// 分的次数循环

if ((sum - 1) % 5 == 0 && j < 5) {// 如果扔一个后能均分5份,继续分

sum = (sum - 1) / 5 * 4;// 每分一次剩余桃子数

if (j == 4) {// 如果已分5次,且仍能除尽,输出,退出程序

System.out.println(i);

System.exit(0);

}

}

}

}

}

}

【程序42】   题目:809*??=800*??+9*??+1。其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。

(本题为无解,去掉1有解)

public class Demo42 {

public static void main(String[] args) {

for (int i = 10; i < 100; i++) {

if (809 * i == (800 * i + 9 * i + 1) && 8 * i >= 10 && 8 * i < 100

&& 9 * i >= 100 && 9 * i < 1000) {

System.out.println(“?? =” + i);

System.out.println(“809*??=”+ 809 * i);

System.exit(0);

}

}

}

}

【程序43】   题目:求0—7所能组成的奇数个数。

暴力算法:

public class Demo43 {

public static boolean isJiShu(int n) {

if (n % 2 != 0) {

return true;

} else {

return false;

}

}

public static boolean fun(char c) {

if (c >= ‘0’ && c <= ‘7’) {

return true;

} else {

return false;

}

}

public static void main(String[] args) {

int count = 0;

String s;

for (int i = 0; i < 100000000; i++) {

s = “” + i;

boolean flag = true;

char[] c = s.toCharArray();

for (int j = 0; j < c.length; j++) {

if (!fun(c[j])) {

flag = false;

break;

}

}

if (flag && isJiShu(i)) {

count++;

}

s = “”;

}

System.out.println(“共” + count + “个。”);

}

}

数学算法:

public class Demo43 {

public static void main(String[] args) {

// 因为是奇数,所以个位只能是1,3,5,7共4种,前面可随便排列

int count = 4;// 个位的4种

// 2位时,十位有8种,个位4种,8×4

// 3位时,8×8×4……

for (int i = 1; i < 8; i++) {

count = 8 * count;

System.out.println(“count:” + count);

}

}

}

个人算法:

//组成1位数是4个。

//组成2位数是7*4个。

//组成3位数是7*8*4个。

//组成4位数是7*8*8*4个。

//…

public class Demo43 {

public static void main (String[] args) {

int sum=4;

int j;

System.out.println(“组成1位数是 “+sum+” 个”);

sum=sum*7;

System.out.println(“组成2位数是 “+sum+” 个”);

for(j=3;j<=9;j++){

sum=sum*8;

System.out.println(“组成”+j+“位数是 “+sum+” 个”);

}

}

}

【程序44】   题目:一个偶数总能表示为两个素数之和。(注:哥德巴赫猜想是想证明对任何大于6的自然数n之内的所有偶数可以表示为两个素数之和)

public class Demo44 {

public static boolean isSuShu(int x) {

if (x == 0 || x == 1) {

return false;

}

for (int i = 2; i <= Math.sqrt(x); i++) {

if (x % i == 0) {

return false;

}

}

return true;

}

public static void main(String[] args) {

// 求了下100以内的情况

for (int i = 0; i < 100; i = i + 2) {

for (int j = 0; j <= (i + 1) / 2; j++) {

if (isSuShu(j) && isSuShu(i - j)) {

System.out.println(i + “=” + j + “+” + (i - j));

}

}

}

}

}

public class Demo44{

public static void main(String[] args){

for (int i=6;i<=100 ;i+=2 ){

for (int j=2;j<100 ;j++ ){

if(!isPrime(j)||!isPrime(i-j)||j>=i)

continue;

System.out.println(i+“=”+j+“+”+(i-j));

break;

}

}

}

public static boolean isPrime(int n){

for (int i=2;i<n ;i++ ){

if(n%i==0)return false;

}

return true;

}

}

【程序45】   题目:(1)判断几个9能被一个素数整除。(2)判断一个整数能被几个9整除。(原题:一个素数能被几个9整除)

(一)

public class Demo45 {

public static boolean isSuShu(int x) {

if (x == 0 || x == 1) {

return false;

}

for (int i = 2; i <= Math.sqrt(x); i++) {

if (x % i == 0) {

return false;

}

}

return true;

}

public static void main(String[] args) {

int[] a = new int[100];

int n = 0;

int num = 0;

// 长度100的素数数组

while (n < 100) {

if (isSuShu(num)) {

a[n] = num;

n++;

num++;

} else {

num++;

}

}

/* for (int t : a) {

System.out.println(t);

}*/

String s = “9”;

int index = 0;

while (s.length() < 9) {

if (new Integer(s).intValue() % a[index] == 0) {

System.out.println(s + “%” + a[index] + “=0”);

if (index < 100 - 1) {

index++;

} else {

index = 0;

s = s + “9”;

}

// System.exit(0);

} else {

if (index < 100 - 1) {

index++;

} else {

index = 0;

s = s + “9”;

}

}

}

}

}

(二)

import java.util.*; public class Demo45 {

public static void main (String[] args) {

Scanner in = new Scanner(System.in);

System.out.print(“请输入一个整数:”);

int num = in.nextInt();

int tmp = num;

int count = 0;

for(int i = 0 ; tmp%9 == 0 😉{

tmp = tmp/9;

count ++;

}

System.out.println(num+" 能够被 “+count+” 个9 整除。");

最后

由于篇幅原因,就不多做展示了

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

//组成3位数是7*8*4个。

//组成4位数是7*8*8*4个。

//…

public class Demo43 {

public static void main (String[] args) {

int sum=4;

int j;

System.out.println(“组成1位数是 “+sum+” 个”);

sum=sum*7;

System.out.println(“组成2位数是 “+sum+” 个”);

for(j=3;j<=9;j++){

sum=sum*8;

System.out.println(“组成”+j+“位数是 “+sum+” 个”);

}

}

}

【程序44】   题目:一个偶数总能表示为两个素数之和。(注:哥德巴赫猜想是想证明对任何大于6的自然数n之内的所有偶数可以表示为两个素数之和)

public class Demo44 {

public static boolean isSuShu(int x) {

if (x == 0 || x == 1) {

return false;

}

for (int i = 2; i <= Math.sqrt(x); i++) {

if (x % i == 0) {

return false;

}

}

return true;

}

public static void main(String[] args) {

// 求了下100以内的情况

for (int i = 0; i < 100; i = i + 2) {

for (int j = 0; j <= (i + 1) / 2; j++) {

if (isSuShu(j) && isSuShu(i - j)) {

System.out.println(i + “=” + j + “+” + (i - j));

}

}

}

}

}

public class Demo44{

public static void main(String[] args){

for (int i=6;i<=100 ;i+=2 ){

for (int j=2;j<100 ;j++ ){

if(!isPrime(j)||!isPrime(i-j)||j>=i)

continue;

System.out.println(i+“=”+j+“+”+(i-j));

break;

}

}

}

public static boolean isPrime(int n){

for (int i=2;i<n ;i++ ){

if(n%i==0)return false;

}

return true;

}

}

【程序45】   题目:(1)判断几个9能被一个素数整除。(2)判断一个整数能被几个9整除。(原题:一个素数能被几个9整除)

(一)

public class Demo45 {

public static boolean isSuShu(int x) {

if (x == 0 || x == 1) {

return false;

}

for (int i = 2; i <= Math.sqrt(x); i++) {

if (x % i == 0) {

return false;

}

}

return true;

}

public static void main(String[] args) {

int[] a = new int[100];

int n = 0;

int num = 0;

// 长度100的素数数组

while (n < 100) {

if (isSuShu(num)) {

a[n] = num;

n++;

num++;

} else {

num++;

}

}

/* for (int t : a) {

System.out.println(t);

}*/

String s = “9”;

int index = 0;

while (s.length() < 9) {

if (new Integer(s).intValue() % a[index] == 0) {

System.out.println(s + “%” + a[index] + “=0”);

if (index < 100 - 1) {

index++;

} else {

index = 0;

s = s + “9”;

}

// System.exit(0);

} else {

if (index < 100 - 1) {

index++;

} else {

index = 0;

s = s + “9”;

}

}

}

}

}

(二)

import java.util.*; public class Demo45 {

public static void main (String[] args) {

Scanner in = new Scanner(System.in);

System.out.print(“请输入一个整数:”);

int num = in.nextInt();

int tmp = num;

int count = 0;

for(int i = 0 ; tmp%9 == 0 😉{

tmp = tmp/9;

count ++;

}

System.out.println(num+" 能够被 “+count+” 个9 整除。");

最后

[外链图片转存中…(img-Oun0kZDS-1715639852338)]

[外链图片转存中…(img-AqZchtgR-1715639852338)]

[外链图片转存中…(img-g1Maz9Ei-1715639852338)]

[外链图片转存中…(img-TD4i76Ni-1715639852339)]

[外链图片转存中…(img-5ixh7gPs-1715639852339)]

[外链图片转存中…(img-3CX0phlp-1715639852339)]

由于篇幅原因,就不多做展示了

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 15
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值