我整理了50道经典Java算法题,直接进了字节跳动!!

左手的ㄟ右手 2021-09-02 06:18 416阅读 0赞

写在前面

最近,很多小伙伴都想进入字节跳动这个快速发展的公司,而字节跳动对于算法的要求比较高。于是乎,有些小伙伴问我能否整理一些基础的算法题,帮助他们提升下基础算法能力。我:没问题啊!于是,经过半个多月的收集和整理,最终输出了这50道经典的Java算法题。

【程序1】

题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

程序分析:兔子的规律为数列1,1,2,3,5,8,13,21…

具体分析如下:

f(1) = 1(第1个月有一对兔子)

f(2) = 1(第2个月还是一对兔子)

f(3) = 2(原来有一对兔子,第3个开始,每个月生一对兔子)

f(4) = 3(原来有两对兔子,有一对可以生育)

f(5) = 5(原来有3对兔子,第3个月出生的那对兔子也可以生育了,那么现在有两对兔子可以生育)

f(6) = 8(原来有5对兔子,第4个月出生的那对兔子也可以生育了,那么现在有3对兔子可以生育)

由以上可以看出,第n个月兔子的对数为

  1. f(n) = f(n - 1) + f(n - 2);

f(n-1)是上个月的兔子数量,是原来有的。

f(n-2)是可以生育的兔子数,即多出来的数量。第n-2个月开始后的第3个月是第n个月,此时第n-2个月时的兔子都可以生育了。

  1. public class Demo01 {
  2. public static void main(String args[]) {
  3. for (int i = 1; i <= 20; i++)
  4. System.out.println(f(i));
  5. }
  6. public static int f(int x) {
  7. if (x == 1||x == 2)
  8. return 1;
  9. else
  10. return f(x - 1) + f(x - 2);
  11. }
  12. }

  1. public class Demo01 {
  2. public static void main(String args[]) {
  3. math mymath = new math();
  4. for (int i = 1; i <= 20; i++)
  5. System.out.println(mymath.f(i));
  6. }
  7. }
  8. class math {
  9. public int f(int x) {
  10. if (x == 1||x == 2)
  11. return 1;
  12. else
  13. return f(x - 1) + f(x - 2);
  14. }
  15. }

【程序2】

题目:判断101-200之间有多少个素数,并输出所有素数。

程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

  1. public class Demo02{
  2. public static void main(String[] args){
  3. for(int i=2;i<=200;i++){
  4. boolean flag=true;
  5. for(int j=2;j<i;j++){
  6. if(i%j==0){
  7. flag=false;
  8. break;
  9. }
  10. }
  11. if(flag==true){
  12. System.out.print(" "+i);
  13. }
  14. }
  15. }
  16. }

【程序3】

题目:打印出所有的 水仙花数 ,所谓 水仙花数 是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 水仙花数 ,因为153=1的三次方+5的三次方+3的三次方。

程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

  1. public class Demo03 {
  2. public static void main(String args[]) {
  3. math mymath = new math();
  4. for (int i = 100; i <= 999; i++)
  5. if (mymath.shuixianhua(i) == true)
  6. System.out.println(i);
  7. }
  8. }
  9. class math {
  10. public boolean shuixianhua(int x) {
  11. int i = 0, j = 0, k = 0;
  12. i = x/100;
  13. j = (x%100)/10;
  14. k = x%10;
  15. if (x == i*i*i + j*j*j + k*k*k)
  16. return true;
  17. else
  18. return false;
  19. }
  20. }

【程序4】

题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

程序分析:对n进行分解质因数,应先找到一个最小的质数i,然后按下述步骤完成:

(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。

(2)如果n > i,但n能被i整除,则应打印出i的值,并用n除以i的商,作为新的正整数你,重复执行第一步。

(3)如果n不能被i整除,则用i+1作为i的值,重复执行第一步。

  1. import java.util.Scanner;
  2. public class Demo04 {
  3. public Demo04() {
  4. super();
  5. }
  6. public void fenjie(int n) {
  7. for (int i = 2; i <= n; i++) {
  8. if (n % i == 0) {
  9. System.out.print(i);
  10. if(n!=i){
  11. System.out.print("*");
  12. }
  13. fenjie(n/i);
  14. }
  15. }
  16. }
  17. public static void main(String[] args) {
  18. Scanner in = new Scanner(System.in);
  19. System.out.println("请输入N的值:");
  20. int N = in.nextInt();
  21. System.out.print( "分解质因数:" + N +"=");
  22. new Demo04().fenjie(N);
  23. }
  24. }

【程序5】

题目:利用条件运算符的嵌套来完成此题:学习成绩=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

程序分析:(a>b)?a:b这是条件运算符的基本例子。

  1. import java.util.Scanner;
  2. public class Demo05 {
  3. public static void main(String[] args) {
  4. System.out.println("请输入N的值:");
  5. Scanner in = new Scanner(System.in);
  6. int N = in.nextInt();
  7. System.out.println(N >= 90 ?"A": (N >= 60 ? "B":"C"));
  8. }
  9. }

【程序6】

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

程序分析:利用辗除法。

  1. import java.util.Scanner;
  2. public class Demo06 {
  3. public static void main(String[] args){
  4. int a,b,m,n;
  5. Scanner in=new Scanner(System.in);
  6. System.out.println("请输入一个正整数:");
  7. a=in.nextInt();
  8. System.out.println("再输入一个正整数:");
  9. b=in.nextInt();
  10. commonDivisor use=new commonDivisor();
  11. m=use.commonDivisor(a,b);
  12. n=a*b/m;
  13. System.out.println("最大公约数:"+m);
  14. System.out.println("最小公倍数:"+n);
  15. }
  16. }
  17. class commonDivisor{
  18. public int commonDivisor(int x,int y){
  19. if(x<y){
  20. int t=x;
  21. x=y;
  22. y=t;
  23. }
  24. while(y!=0){
  25. if(x==y)return x;
  26. else{
  27. int k=x%y;
  28. x=y;
  29. y=k;
  30. }
  31. }
  32. return x;
  33. }
  34. }

【程序7】

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

程序分析:利用for循环语句,if条件语句。

  1. import java.util.Scanner;
  2. public class Demo07 {
  3. public static void main(String[] args){
  4. System.out.println("请输入一个字符串;");
  5. Scanner in=new Scanner(System.in);
  6. String str=in.nextLine();
  7. char[] ch=str.toCharArray();
  8. count use=new count();
  9. use.count(ch);
  10. }
  11. }
  12. class count{
  13. int digital,character,blank,other;
  14. public void count(char[] arr){
  15. for(int i=0;i<arr.length;i++){
  16. if(arr[i]>='0'&&arr[i]<='9'){
  17. digital++;
  18. }else if((arr[i]>='a'&&arr[i]<='z')||(arr[i]>='A'&&arr[i]<='Z')){
  19. character++;
  20. }else if(arr[i]==' '){
  21. blank++;
  22. }else{
  23. other++;
  24. }
  25. }
  26. System.out.println("数字个数:"+digital);
  27. System.out.println("英文字母个数:"+character);
  28. System.out.println("空格个数:"+blank);
  29. System.out.println("其他字符个数:"+other);
  30. }
  31. }

【程序8】

题目:求s = a + aa + aaa + aaaa + aa…a的值,其中a是一个数字。例如2 + 22 + 222 + 2222 + 22222(此时共有5个数相加),几个数相加有键盘控制。

程序分析:关键是计算出每一项的值。

  1. import java.util.Scanner;
  2. public class Demo08 {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.println(请输入a的值);
  6. int a = in.nextInt();
  7. System.out.println(请输入n个数);
  8. int n = in.nextInt();
  9. int s = 0,t=0;
  10. for (int i = 1; i <= n; i++) {
  11. t += a;
  12. a = a*10;
  13. s += t;
  14. }
  15. System.out.println(s);
  16. }
  17. }

【程序9】

题目:一个数如果恰好等于它的因子之和,这个数就称为”完数”。例如6=1+2+3。编程找出1000以内的所有完数。

  1. public class Demo09 {
  2. public static void main(String[] args) {
  3. int s;
  4. for (int i = 1; i <= 1000; i++) {
  5. s = 0;
  6. for (int j = 1; j < i; j++)
  7. if (i % j == 0)
  8. s = s + j;
  9. if (s == i)
  10. System.out.print(i + " " );
  11. }
  12. System.out.println();
  13. }
  14. }

  1. public class Demo09{
  2. public static void main(String[] args) {
  3. int i,j,sum;
  4. for(i=1;i<1000;i++)
  5. {
  6. sum = 0;
  7. for(j=1;j<=i/2;j++)
  8. {
  9. if(i%j==0)
  10. {
  11. sum+=j;
  12. }
  13. }
  14. if(sum==i)
  15. {
  16. System.out.print(i+" its factors are: ");
  17. for(j=1;j<=i/2;j++)
  18. {
  19. if(i%j==0)
  20. System.out.print(j+", ");
  21. }
  22. System.out.println();
  23. }
  24. }
  25. }
  26. }

【程序10】

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

  1. public class Demo10 {
  2. public static void main(String[] args) {
  3. double s = 0;
  4. double h = 100;
  5. for (int i = 1; i <= 10; i++) {
  6. s += h;
  7. h = h/2;
  8. s += h;
  9. }
  10. System.out.println("经过路程:"+s);
  11. System.out.println("反弹高度:"+h);
  12. }
  13. }

【程序11】

题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。

  1. public class Demo11 {
  2. public static void main(String[] args) {
  3. int count = 0;
  4. for (int i = 1; i <= 4; i++)
  5. for (int j = 1; j <= 4; j++)
  6. for (int k = 1; k <= 4; k++)
  7. if (i != j && j != k && i != k) {
  8. count += 1;
  9. System.out.println(i*100 + j*10 + k);
  10. }
  11. System.out.println("共" + count + "个三位数");
  12. }
  13. }

【程序12】

题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润lirun,求应发放奖金总数sum?

程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

  1. import java.util.Scanner;
  2. public class Demo12 {
  3. public static void main(String[] args) {
  4. double sum;
  5. System.out.println("输入当月利润:(万元)");
  6. Scanner in = new Scanner(System.in);
  7. double lirun = in.nextDouble();
  8. if (lirun <= 10) {
  9. sum = lirun * 0.1;
  10. } else if (lirun <= 20) {
  11. sum = 10*0.1 + (lirun - 10) * 0.075;
  12. } else if (lirun <= 40) {
  13. sum = 10*0.1 + 10*0.075 + (lirun - 20) * 0.05;
  14. } else if (lirun <= 60) {
  15. sum = 10*0.1 + 10*0.075 + 10*0.05 + (lirun - 40) * 0.03;
  16. } else if (lirun <= 100) {
  17. sum = 10*0.1 + 10*0.075 + 10*0.05 + 10*0.03 + (lirun - 60) * 0.015;
  18. } else {
  19. sum = 10*0.1 + 10*0.075 + 10*0.05 + 10*0.03 + 10*0.015 + (lirun - 100) * 0.01;
  20. }
  21. System.out.println("应发的奖金是:"+sum+"(万元)");
  22. }
  23. }

【程序13】

题目:一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?

程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上168后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析:

  1. public class Demo13 {
  2. public static void main(String[] args) {
  3. for(int x=1;x<100000;x++){
  4. if(Math.sqrt(x+100)%1==0)
  5. if(Math.sqrt(x+100+168)%1==0)
  6. System.out.println(x+"加上100后是一个完全平方数,加上168又是一个完全平方数");
  7. }
  8. }
  9. }

【程序14】

题目:输入某年某月某日,判断这一天是这一年的第几天?

程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本月的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

  1. import java.util.Calendar;
  2. import java.util.Scanner;
  3. public class Demo14 {
  4. public static void main(String[] args) {
  5. System.out.println("请输入年,月,日:");
  6. Scanner in = new Scanner(System.in);
  7. int year = in.nextInt();
  8. int month = in.nextInt();
  9. int day = in.nextInt();
  10. Calendar cal = Calendar.getInstance();
  11. cal.set(year, month - 1, day);
  12. int sum = cal.get(Calendar.DAY_OF_YEAR);
  13. System.out.println("这一天是这一年的第" + sum +"天");
  14. }
  15. }

  1. import java.util.*;
  2. public class Demo14 {
  3. public static void main(String[] args){
  4. int year,month,day,sum=0;
  5. Scanner in=new Scanner(System.in);
  6. System.out.println("输入年:");
  7. year=in.nextInt();
  8. System.out.println("输入月:");
  9. month=in.nextInt();
  10. System.out.println("输入日:");
  11. day=in.nextInt();
  12. switch(month){
  13. case 1:
  14. sum=0;
  15. break;
  16. case 2:
  17. sum=31;
  18. break;
  19. case 3:
  20. sum=59;
  21. break;
  22. case 4:
  23. sum=90;
  24. break;
  25. case 5:
  26. sum=120;
  27. break;
  28. case 6:
  29. sum=151;
  30. break;
  31. case 7:
  32. sum=181;
  33. break;
  34. case 8:
  35. sum=212;
  36. break;
  37. case 9:
  38. sum=243;
  39. break;
  40. case 10:
  41. sum=273;
  42. break;
  43. case 11:
  44. sum=304;
  45. break;
  46. case 12:
  47. sum=334;
  48. break;
  49. default:
  50. System.out.println("wrong input!");
  51. return;
  52. }
  53. sum=sum+day;
  54. boolean leap;
  55. if(year%400==0||(year%4==0&&year%100!=0)){
  56. leap=true;
  57. }else {
  58. leap=false;
  59. }
  60. if(leap&&month>2){
  61. sum++;
  62. }
  63. System.out.println("It is the "+sum+"th day.");
  64. }
  65. }

  1. import java.util.Scanner;
  2. public class Demo14 {
  3. public static void main(String[] args){
  4. System.out.println("请输入年 月 日:");
  5. Scanner in=new Scanner(System.in);
  6. int year=in.nextInt();
  7. int month=in.nextInt();
  8. int day=in.nextInt();
  9. System.out.println("是该年的第"+count(year,month,day)+"天");
  10. }
  11. public static int count(int year,int month,int day){
  12. int sum=0;
  13. int days=0;
  14. for(int i=1;i<month;i++){
  15. switch(i){
  16. case 1:
  17. case 3:
  18. case 5:
  19. case 7:
  20. case 8:
  21. case 10:
  22. case 12:
  23. days=31;
  24. break;
  25. case 4:
  26. case 6:
  27. case 9:
  28. case 11:
  29. days=30;
  30. break;
  31. case 2:
  32. if(year%400==0||year%4==0&&year%100!=0){
  33. days=29;
  34. }else{
  35. days=28;
  36. }
  37. break;
  38. }
  39. sum+=days;
  40. }
  41. sum+=day;
  42. return sum;
  43. }
  44. }

【程序15】

题目:输入三个整数x,y,z,请把这三个数由小到大输出。

程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class Demo15 {
  4. public static void main(String[] args) {
  5. System.out.print("请输入三个数:");
  6. Scanner in = new Scanner(System.in);
  7. int[] arr = new int[3];
  8. for (int i = 0; i < 3; i++) {
  9. arr[i] = in.nextInt();
  10. }
  11. Arrays.sort(arr);
  12. for (int i=0;i<arr.length;i++) {
  13. System.out.print(arr[i] + " ");
  14. }
  15. }
  16. }

  1. if(x > y) {
  2. int t = x; x = y; y = t;
  3. }
  4. if(x > z) {
  5. int t = x; x = z; z = t;
  6. }
  7. if(y > z) {
  8. int t = y; y = z; z = t;
  9. }

【程序16】

题目:输出9*9口诀乘法表。

程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

出现重复的乘积(全矩形)

  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 <= 9; j++)
  5. System.out.print(i + "*" + j + "=" + (i*j) + "\t");
  6. System.out.println();
  7. }
  8. }
  9. }

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

  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. 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】

题目:打印出如下图案(菱形)

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

三角形:

  1. *
  2. ***
  3. ******
  4. ********
  5. ******
  6. ***
  7. *
  8. public class Demo19 {
  9. public static void main(String[] args) {
  10. int i=0;
  11. int j=0;
  12. for ( i = 1; i <= 4; i++) {
  13. for ( j = 1; j <= 2 * i - 1; j++)
  14. System.out.print("*");
  15. System.out.println();
  16. }
  17. for ( i = 3; i >= 1; i--) {
  18. for ( j = 1; j <= 2 * i - 1; j++)
  19. System.out.print("*");
  20. System.out.println();
  21. }
  22. }
  23. }

菱形:

  1. *
  2. ***
  3. *****
  4. *******
  5. *****
  6. ***
  7. *
  8. public class Demo19 {
  9. public static void main(String[] args) {
  10. int i = 0;
  11. int j = 0;
  12. for (i = 1; i <= 4; 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. for (i = 3; i >= 1; i--) {
  20. for (int k = 1; k <= 4 - i; k++)
  21. System.out.print( " " );
  22. for (j = 1; j <= 2 * i - 1; j++)
  23. System.out.print("*");
  24. System.out.println();
  25. }
  26. }
  27. }

【程序20】

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

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

  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. 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!。

程序分析:递归公式: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岁。请问第五个人多大?

程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(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】

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

程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或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个数进行排序。

程序分析:可以利用选择法,即从后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矩阵主对角线元素之和。

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

  1. public class Demo29 {
  2. public static void main(String[] args) {
  3. double sum = 0;
  4. int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 7, 8 } };
  5. for (int i = 0; i < 3; i++)
  6. for (int j = 0; j < 3; j++) {
  7. if (i == j)
  8. sum = sum + array[i][j];
  9. }
  10. System.out.println(sum);
  11. }
  12. }

主负对角线:

  1. for(i=0;i<n;i++)
  2. for(j=0;j<n;j++)
  3. {
  4. if(i==j) sum1+=a[i][j];
  5. if(i+j==n-1) sum2+=a[i][j];
  6. }

【程序30】

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

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

  1. import java.util.Random;
  2. public class Demo30 {
  3. public static void main(String[] args) {
  4. int temp = 0;
  5. int arr[] = new int[12];
  6. Random r = new Random();
  7. for (int i = 0; i <= 10; i++)
  8. arr[i] = r.nextInt(1000);
  9. for (int i = 0; i <= 10; i++)
  10. System.out.print(arr[i] + "\t");
  11. for (int i = 0; i <= 9; i++)
  12. for (int k = i + 1; k <= 10; k++)
  13. if (arr[i] > arr[k]) {
  14. temp = arr[i];
  15. arr[i] = arr[k];
  16. arr[k] = temp;
  17. }
  18. System.out.println();
  19. for (int k = 0; k <= 10; k++)
  20. System.out.print(arr[k] + "\t");
  21. arr[11] = r.nextInt(1000);
  22. for (int k = 0; k <= 10; k++)
  23. if (arr[k] > arr[11]) {
  24. temp = arr[11];
  25. for (int j = 11; j >= k + 1; j--)
  26. arr[j] = arr[j - 1];
  27. arr[k] = temp;
  28. }
  29. System.out.println();
  30. for (int k = 0; k <= 11; k++)
  31. System.out.print(arr[k] + "\t");
  32. }
  33. }

【程序31】

题目:将一个数组逆序输出。

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

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

  1. public class Demo31 {
  2. public static void main(String[] args) {
  3. int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  4. for (int i = a.length - 1; i >= 0; i--) {
  5. System.out.print(a[i] + " ");
  6. }
  7. }
  8. }

【程序32】

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

  1. import java.util.*;
  2. public class Demo32 {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.print("请输入一个7位以上的正整数:");
  6. long l = in.nextLong();
  7. String str = Long.toString(l);
  8. char[] ch = str.toCharArray();
  9. int j=ch.length;
  10. if (j<7){ System.out.println("输入错误!");
  11. } else {
  12. System.out.println("截取从右端开始的4~7位是:"+ch[j-7]+ch[j-6]+ch[j-5]+ch[j-4]);
  13. }
  14. }
  15. }

  1. import java.util.Scanner;
  2. public class Demo32{
  3. public static void main(String[] args) {
  4. int a = 0;
  5. Scanner s = new Scanner(System.in);
  6. long b = s.nextLong();
  7. a = (int) (b % 10000000 / 1000);
  8. System.out.println(a);
  9. }
  10. }

【程序33】

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

程序分析:

  1. 1
  2. 1 1
  3. 1 2 1
  4. 1 3 3 1
  5. 1 4 6 4 1
  6. 1 5 10 10 5 1
  7. public class Demo33 {
  8. public static void main(String args[]) {
  9. int i, j;
  10. int a[][];
  11. int n = 10;
  12. a = new int[n][n];
  13. for (i = 0; i < n; i++) {
  14. a[i][i] = 1;
  15. a[i][0] = 1;
  16. }
  17. for (i = 2; i < n; i++) {
  18. for (j = 1; j <= i - 1; j++) {
  19. a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
  20. }
  21. }
  22. for (i = 0; i < n; i++) {
  23. for (j = 0; j <= i; j++) {
  24. System.out.printf(a[i][j] + "\t");
  25. }
  26. System.out.println();
  27. }
  28. }
  29. }

【程序34】

题目:输入3个数a,b,c,按大小顺序输出。(也可互相比较交换排序)

  1. import java.util.Arrays;
  2. public class Demo34 {
  3. public static void main(String[] args) {
  4. int[] arrays = { 800, 56, 500 };
  5. Arrays.sort(arrays);
  6. for (int n = 0; n < arrays.length; n++)
  7. System.out.println(arrays[n]);
  8. }
  9. }

  1. if(x > y) {
  2. int t = x; x = y;
  3. y = t;
  4. } if(x > z) {
  5. int t = x;
  6. x = z;
  7. z = t;
  8. } if(y > z) {
  9. int t = y;
  10. y = z;
  11. z = t;
  12. }

【程序35】

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

  1. import java.util.*;
  2. public class Demo35 {
  3. public static void main(String[] args) {
  4. int i, min=0, max=0, n, temp1, temp2;
  5. int a[];
  6. System.out.println("定义数组的长度:");
  7. Scanner in = new Scanner(System.in);
  8. n = in.nextInt();
  9. a = new int[n];
  10. for (i = 0; i < n; i++) {
  11. System.out.print("输入第" + (i + 1) + "个数据:");
  12. a[i] = in.nextInt();
  13. }
  14. for (i = 1; i < n; i++) {
  15. if (a[i] > a[max])
  16. max = i;
  17. if (a[i] < a[min])
  18. min = i;
  19. }
  20. temp1 = a[0];
  21. a[0] = a[max];
  22. a[max] = temp1;
  23. temp2 = a[min];
  24. if (min != 0) { // 如果最小值不是a[0],执行下面
  25. a[min] = a[n - 1];
  26. a[n - 1] = temp2;
  27. } else { //如果最小值是a[0],执行下面
  28. a[max] = a[n - 1];
  29. a[n - 1] = temp1;
  30. }
  31. for (i = 0; i < n; i++) {
  32. System.out.print(a[i] + " " );
  33. }
  34. }
  35. }

【程序36】

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

  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. public class Demo36 {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. System.out.println("输入数字个数n:");
  8. int n = in.nextInt();
  9. System.out.println("输入后移位数m:");
  10. int m = in.nextInt();
  11. LinkedList<Integer> list = new LinkedList<Integer>();
  12. for (int i = 0; i < n; i++) {
  13. System.out.println("请输入第"+(i+1)+"个数:");
  14. list.add(in.nextInt());
  15. }
  16. System.out.println("原数据排序为:");
  17. for (int t : list) {
  18. System.out.print(t + " " );
  19. }
  20. System.out.println();
  21. List<Integer> temp1 = list.subList(list.size() - m, list.size());
  22. List<Integer> temp2 = list.subList(0, list.size() - m);
  23. temp2.addAll(0, temp1);
  24. System.out.println("移动后排序为;");
  25. for (int t : temp2) {
  26. System.out.print(t + " " );
  27. }
  28. }
  29. }

  1. import java.util.*;
  2. public class Demo36{
  3. public static void main(String[] args){
  4. Scanner in=new Scanner(System.in);
  5. System.out.println("请定义数组的长度:");
  6. int n=in.nextInt();
  7. System.out.println("请输入移动的位数:");
  8. int m=in.nextInt();
  9. int [] arr=new int [n];
  10. int [] brr=new int [n];
  11. for(int i=0;i<n;i++){
  12. System.out.println("请输入第"+(i+1)+"个数:");
  13. arr[i]=in.nextInt();
  14. }
  15. System.out.println("排序前:");
  16. for(int i=0;i<n;i++){
  17. System.out.print(arr[i]+" ");
  18. }
  19. System.out.println();
  20. for(int i=0;i<m;i++){
  21. brr[i]=arr[n-m+i];
  22. }
  23. for(int i=0;i<n-m;i++){
  24. arr[m+i]=arr[i];
  25. }
  26. for(int i=0;i<m;i++){
  27. arr[i]=brr[i];
  28. }
  29. System.out.println("排序后:");
  30. for(int i=0;i<n;i++){
  31. System.out.print(arr[i]+" ");
  32. }
  33. }
  34. }

【程序37】

题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。(约瑟夫环问题,百度百科有时间复杂度最简单的数学方法)

原例代码:

  1. import java.util.Scanner;
  2. public class Demo37 {
  3. public static void main(String[] args) {
  4. System.out.println("请输人数n:");
  5. Scanner in = new Scanner(System.in);
  6. int n = in.nextInt();
  7. boolean[] arr = new boolean[n];
  8. for (int i = 0; i < arr.length; i++) {
  9. arr[i] = true; //下标为TRUE时说明还在圈里
  10. }
  11. int leftCount = n;
  12. int countNum = 0;
  13. int index = 0;
  14. while (leftCount > 1) {
  15. if (arr[index] == true) { //当在圈里时
  16. countNum++; //报数递加
  17. if (countNum == 3) { //报数为3时
  18. countNum = 0; //从零开始继续报数
  19. arr[index] = false; //此人退出圈子
  20. leftCount--; //剩余人数减一
  21. }
  22. }
  23. index++; //每报一次数,下标加一
  24. if (index == n) { //是循环数数,当下标大于n时,说明已经数了一圈,
  25. index = 0; //将下标设为零重新开始。
  26. }
  27. }
  28. for (int i = 0; i < n; i++) {
  29. if (arr[i] == true) {
  30. System.out.println(i);
  31. }
  32. }
  33. }
  34. }

个人代码1:

  1. import java.util.Scanner;
  2. public class Demo37 {
  3. public static void main(String[] args) {
  4. System.out.println("请输入人数:");
  5. Scanner in = new Scanner(System.in);
  6. int[] a = new int[in.nextInt()];
  7. for (int i = 0; i < a.length; i++) {
  8. a[i] = 1;
  9. }
  10. int left = a.length;
  11. int j = 0;
  12. int num = 0;
  13. while (left > 1) {
  14. if (a[j] == 1) {
  15. num++;
  16. }
  17. if (num == 3) {
  18. a[j] = 0;
  19. num = 0;
  20. left--;
  21. }
  22. j++;
  23. if (j == a.length) {
  24. j = 0;
  25. }
  26. }
  27. for (int i = 0; i < a.length; i++) {
  28. if (a[i] == 1) {
  29. System.out.println("最后留下的人是"+ (i + 1) + "号");
  30. break;
  31. }
  32. }
  33. }
  34. }

个人代码2:

  1. import java.util.LinkedList;
  2. import java.util.Scanner;
  3. public class Demo37 {
  4. public static void main(String[] args) {
  5. LinkedList<Integer> l = new LinkedList<Integer>();
  6. System.out.println("请输入人数:");
  7. Scanner in = new Scanner(System.in);
  8. int len = in.nextInt();
  9. for (int i = 0; i < len; i++) {
  10. l.add(i + 1);
  11. }
  12. int sum = 0;
  13. int temp = 0;
  14. for (int i = 0; sum != len - 1;) {
  15. if (l.get(i) != 0) {
  16. temp++;
  17. }
  18. if (temp == 3) {
  19. l.remove(i);
  20. l.add(i, 0);
  21. temp = 0;
  22. sum++;
  23. }
  24. i++;
  25. if (i == l.size()) {
  26. i = 0;
  27. }
  28. }
  29. for (int t : l) {
  30. if (t != 0) {
  31. System.out.println("最后留下的人是" + t + "号");
  32. }
  33. }
  34. }
  35. }

【程序38】

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

  1. import java.util.Scanner;
  2. public class Demo38 {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.println("请输入一个字符串:");
  6. String mys = in.next();
  7. System.out.println(str_len(mys));
  8. }
  9. public static int str_len(String x) {
  10. return x.length();
  11. }
  12. }

  1. import java.util.Scanner;
  2. public class Demo38 {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.println("请输入一个字符串:");
  6. String mys = in.next();
  7. System.out.println(mys.length());
  8. }
  9. }

【程序39】

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

  1. import java.util.Scanner;
  2. public class Demo39 {
  3. public static double ouShu(int n) {
  4. double result = 0;
  5. for (int i = 2; i <= n; i = i + 2) {
  6. result += 1 / (double) i;
  7. }
  8. return result;
  9. }
  10. public static double jiShu(int n) {
  11. double result = 0;
  12. for (int i = 1; i <= n; i = i + 2) {
  13. result += 1 / (double) i;
  14. }
  15. return result;
  16. }
  17. public static void main(String[] args) {
  18. Scanner in = new Scanner(System.in);
  19. System.out.println("输入n的值:");
  20. int n = in.nextInt();
  21. if (n % 2 == 0) { //偶数,1/2+1/4+...+1/n
  22. System.out.println(ouShu(n));
  23. } else { //奇数,1/1+1/3+...+1/n
  24. System.out.println(jiShu(n));
  25. }
  26. }
  27. }

【程序40】

题目:字符串排序。(利用容器类中的sort方法)

  1. import java.util.*;
  2. public class Demo40 {
  3. public static void main(String[] args) {
  4. ArrayList<String> list = new ArrayList<String>();
  5. list.add("010102");
  6. list.add("010003");
  7. list.add("010201");
  8. Collections.sort(list);
  9. for (int i = 0; i < list.size(); i++) {
  10. System.out.println(list.get(i));
  11. }
  12. }
  13. }

  1. import java.util.*;
  2. public class Demo40 {
  3. public static void main(String[] args){
  4. Scanner in=new Scanner(System.in);
  5. System.out.println("请定义字符串的个数:");
  6. int n=in.nextInt();
  7. String[] str=new String[n];
  8. for(int i=0;i<str.length;i++){
  9. System.out.println("请输入第"+(i+1)+"字符串:");
  10. str[i]=in.next();
  11. }
  12. strSort(n,str);
  13. System.out.println("字符串排序后:");
  14. for(int i=0;i<str.length;i++){
  15. System.out.print(str[i]+" ");
  16. }
  17. }
  18. public static void strSort(int n,String[] arr){
  19. for(int i=0; i<n; i++) {
  20. for(int j=i+1; j<n; j++) {
  21. if(compare(arr[i], arr[j]) == false) {
  22. String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
  23. }
  24. }
  25. }
  26. }
  27. static boolean compare(String s1, String s2) {
  28. boolean result = true;
  29. for(int i=0; i<s1.length() && i<s2.length(); i++) {
  30. if(s1.charAt(i) > s2.charAt(i)) {
  31. result = false;
  32. break;
  33. } else if(s1.charAt(i) <s2.charAt(i)) {
  34. result = true;
  35. break;
  36. } else {
  37. if(s1.length() < s2.length()) {
  38. result = true;
  39. } else {
  40. result = false;
  41. }
  42. }
  43. }
  44. return result;
  45. }
  46. }

【程序41】

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

本题源码:

  1. public class Demo41 {
  2. static int ts = 0;// 桃子总数
  3. int fs = 1;// 记录分的次数
  4. static int hs = 5;// 猴子数
  5. int tsscope = 5000;// 桃子数的取值范围,太大容易溢出。
  6. public int fT(int t) {
  7. if (t == tsscope) {
  8. // 当桃子数到了最大的取值范围时取消递归
  9. System.out.println("结束");
  10. return 0;
  11. } else {
  12. if ((t - 1) % hs == 0 && fs <= hs) {
  13. if (fs == hs) {
  14. System.out.println("桃子数=" + ts + "时满足分桃条件");
  15. }
  16. fs += 1;
  17. return fT((t - 1) / 5 * 4);// 返回猴子拿走一份后的剩下的总数
  18. } else {
  19. // 没满足条件
  20. fs = 1;// 分的次数重置为1
  21. return fT(ts += 1);// 桃子数加+1
  22. }
  23. }
  24. }
  25. public static void main(String[] args) {
  26. new Demo41().fT(0);
  27. }
  28. }

个人修改:

  1. public class Demo41 {
  2. public static void main(String[] args) {
  3. int sum = 0;
  4. for (int i = 6;; i++) { // 最少6个分最后一次
  5. sum = i;// 桃子数
  6. for (int j = 0; j < 5; j++) { // 分的次数循环
  7. if ((sum - 1) % 5 == 0 && j < 5) { // 如果扔一个后能均分5份,继续分
  8. sum = (sum - 1) / 5 * 4;// 每分一次剩余桃子数
  9. if (j == 4) { // 如果已分5次,且仍能除尽,输出,退出程序
  10. System.out.println(i);
  11. System.exit(0);
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }

【程序42】

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

  1. public class Demo42 {
  2. public static void main(String[] args) {
  3. for (int i = 10; i < 100; i++) {
  4. if (809 * i == (800 * i + 9 * i + 1) && 8 * i >= 10 && 8 * i < 100
  5. && 9 * i >= 100 && 9 * i < 1000) {
  6. System.out.println("?? =" + i);
  7. System.out.println("809*??="+ 809 * i);
  8. System.exit(0);
  9. }
  10. }
  11. }
  12. }

【程序43】

题目:求0—7所能组成的奇数个数。

暴力算法:

  1. public class Demo43 {
  2. public static boolean isJiShu(int n) {
  3. if (n % 2 != 0) {
  4. return true;
  5. } else {
  6. return false;
  7. }
  8. }
  9. public static boolean fun(char c) {
  10. if (c >= '0' && c <= '7') {
  11. return true;
  12. } else {
  13. return false;
  14. }
  15. }
  16. public static void main(String[] args) {
  17. int count = 0;
  18. String s;
  19. for (int i = 0; i < 100000000; i++) {
  20. s = "" + i;
  21. boolean flag = true;
  22. char[] c = s.toCharArray();
  23. for (int j = 0; j < c.length; j++) {
  24. if (!fun(c[j])) {
  25. flag = false;
  26. break;
  27. }
  28. }
  29. if (flag && isJiShu(i)) {
  30. count++;
  31. }
  32. s = "";
  33. }
  34. System.out.println("共" + count + "个。");
  35. }
  36. }

数学算法:

  1. public class Demo43 {
  2. public static void main(String[] args) {
  3. // 因为是奇数,所以个位只能是1,3,5,7共4种,前面可随便排列
  4. int count = 4;// 个位的4种
  5. // 2位时,十位有8种,个位4种,8×4
  6. // 3位时,8×8×4……
  7. for (int i = 1; i < 8; i++) {
  8. count = 8 * count;
  9. System.out.println("count:" + count);
  10. }
  11. }
  12. }

个人算法:

  1. //组成1位数是4个。
  2. //组成2位数是7*4个。
  3. //组成3位数是7*8*4个。
  4. //组成4位数是7*8*8*4个。
  5. //......
  6. public class Demo43 {
  7. public static void main (String[] args) {
  8. int sum=4;
  9. int j;
  10. System.out.println("组成1位数是 "+sum+" 个");
  11. sum=sum*7;
  12. System.out.println("组成2位数是 "+sum+" 个");
  13. for(j=3;j<=9;j++){
  14. sum=sum*8;
  15. System.out.println("组成"+j+"位数是 "+sum+" 个");
  16. }
  17. }
  18. }

【程序44】

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

  1. public class Demo44 {
  2. public static boolean isSuShu(int x) {
  3. if (x == 0 || x == 1) {
  4. return false;
  5. }
  6. for (int i = 2; i <= Math.sqrt(x); i++) {
  7. if (x % i == 0) {
  8. return false;
  9. }
  10. }
  11. return true;
  12. }
  13. public static void main(String[] args) {
  14. // 求了下100以内的情况
  15. for (int i = 0; i < 100; i = i + 2) {
  16. for (int j = 0; j <= (i + 1) / 2; j++) {
  17. if (isSuShu(j) && isSuShu(i - j)) {
  18. System.out.println(i + "=" + j + "+" + (i - j));
  19. }
  20. }
  21. }
  22. }
  23. }

  1. public class Demo44{
  2. public static void main(String[] args){
  3. for (int i=6;i<=100 ;i+=2 ){
  4. for (int j=2;j<100 ;j++ ){
  5. if(!isPrime(j)||!isPrime(i-j)||j>=i)
  6. continue;
  7. System.out.println(i+"="+j+"+"+(i-j));
  8. break;
  9. }
  10. }
  11. }
  12. public static boolean isPrime(int n){
  13. for (int i=2;i<n ;i++ ){
  14. if(n%i==0)return false;
  15. }
  16. return true;
  17. }
  18. }

【程序45】

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

(一)

  1. public class Demo45 {
  2. public static boolean isSuShu(int x) {
  3. if (x == 0 || x == 1) {
  4. return false;
  5. }
  6. for (int i = 2; i <= Math.sqrt(x); i++) {
  7. if (x % i == 0) {
  8. return false;
  9. }
  10. }
  11. return true;
  12. }
  13. public static void main(String[] args) {
  14. int[] a = new int[100];
  15. int n = 0;
  16. int num = 0;
  17. // 长度100的素数数组
  18. while (n < 100) {
  19. if (isSuShu(num)) {
  20. a[n] = num;
  21. n++;
  22. num++;
  23. } else {
  24. num++;
  25. }
  26. }
  27. /* for (int t : a) { System.out.println(t); }*/
  28. String s = "9";
  29. int index = 0;
  30. while (s.length() < 9) {
  31. if (new Integer(s).intValue() % a[index] == 0) {
  32. System.out.println(s + "%" + a[index] + "=0");
  33. if (index < 100 - 1) {
  34. index++;
  35. } else {
  36. index = 0;
  37. s = s + "9";
  38. }
  39. // System.exit(0);
  40. } else {
  41. if (index < 100 - 1) {
  42. index++;
  43. } else {
  44. index = 0;
  45. s = s + "9";
  46. }
  47. }
  48. }
  49. }
  50. }

(二)

  1. import java.util.*;
  2. public class Demo45 {
  3. public static void main (String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.print("请输入一个整数:");
  6. int num = in.nextInt();
  7. int tmp = num;
  8. int count = 0;
  9. for(int i = 0 ; tmp%9 == 0 ;){
  10. tmp = tmp/9;
  11. count ++;
  12. }
  13. System.out.println(num+" 能够被 "+count+" 个9 整除。");
  14. }
  15. }

【程序46】

题目:两个字符串连接程序。

  1. import java.util.Scanner;
  2. public class Demo46 {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.println("输入第一个字符串:");
  6. String s1 = in.next();
  7. System.out.println("输入第一个字符串:");
  8. String s2 = in.next();
  9. System.out.println("连接后:\n" + s1 + s2);
  10. }
  11. }

  1. import java.util.*;
  2. public class Demo46 {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.print("请输入一个字符串:");
  6. String str1 = in.nextLine();
  7. System.out.print("请再输入一个字符串:");
  8. String str2 = in.nextLine();
  9. String str = str1+str2;
  10. System.out.println("连接后的字符串是:"+str);
  11. }
  12. }

【程序47】

题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的。

  1. import java.util.*;
  2. public class Demo47 {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in);
  5. int n=1,num;
  6. while(n<=7){
  7. do{
  8. System.out.print("请输入一个1--50 之间的整数:");
  9. num= s.nextInt();
  10. }while(num<1||num>50);
  11. for(int i=1;i<=num;i++)
  12. { System.out.print("*");
  13. }
  14. System.out.println();
  15. n ++;
  16. }
  17. }
  18. }

  1. import java.util.Scanner;
  2. public class Demo47 {
  3. public static void print(int n) {
  4. for (int i = 0; i < n; i++) {
  5. System.out.print("*");
  6. }
  7. System.out.println();
  8. }
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. for (int i = 0; i < 7; i++) {
  12. int temp = in.nextInt();
  13. print(temp);
  14. }
  15. }
  16. }

【程序48】

题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

  1. import java.util.Scanner;
  2. public class Demo48{
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.println("请输入一个4位数字:");
  6. String str = in.next();
  7. if (!((str).matches("\\d{4}"))) {
  8. System.out.println("输入的不是4位数字!");
  9. System.exit(0);
  10. }
  11. char[] c = str.toCharArray();
  12. int[] a = new int[4];
  13. for (int i = 0; i < a.length; i++) {
  14. a[i] = ((int) (c[i] - '0') + 5) % 10;
  15. }
  16. int t;
  17. t = a[0];
  18. a[0] = a[3];
  19. a[3] = t;
  20. t = a[1];
  21. a[1] = a[2];
  22. a[2] = t;
  23. System.out.println("结果是:" + a[0] + a[1] + a[2] + a[3]);
  24. }
  25. }

  1. import java.util.*;
  2. public class Demo48 {
  3. public static void main(String args[]) {
  4. Scanner s = new Scanner(System.in);
  5. int num=0,temp;
  6. do{
  7. System.out.print("请输入一个4位正整数:");
  8. num = s.nextInt();
  9. }while (num<1000||num>9999);
  10. int a[]=new int[4];
  11. a[0] = num/1000; //取千位的数字
  12. a[1] = (num/100)%10; //取百位的数字
  13. a[2] = (num/10)%10; //取十位的数字
  14. a[3] = num%10; //取个位的数字
  15. for(int j=0;j<4;j++) {
  16. a[j]+=5; a[j]%=10;
  17. }
  18. for(int j=0;j<=1;j++) {
  19. temp = a[j]; a[j] = a[3-j]; a[3-j] =temp;
  20. }
  21. System.out.print("加密后的数字为:");
  22. for(int j=0;j<4;j++) System.out.print(a[j]);
  23. }
  24. }

【程序49】

题目:计算字符串中子串出现的次数。

  1. import java.util.Scanner;
  2. public class Demo49 {
  3. public static void main(String[] args) {
  4. Scanner in=new Scanner(System.in);
  5. System.out.println("请输入主串:");
  6. String str1 = in.nextLine();
  7. System.out.println("请输入子串:");
  8. String str2 = in.nextLine();
  9. // 生成子串长度的N个字符串数组
  10. String[] sa = new String[str1.length() - str2.length() + 1];
  11. for (int i = 0; i < sa.length; i++) {
  12. sa[i] = str1.substring(i, i + str2.length());
  13. }
  14. int sum = 0;
  15. // 子串与N个拆开的子串比对
  16. for (int i = 0; i < sa.length; i++) {
  17. if (sa[i].equals(str2)) {
  18. // 成功配对,计数器+1;
  19. sum++;
  20. // 因为不计算重叠的子串,所以跳过配对之后的部分拆分子串
  21. i = i + str2.length();
  22. }
  23. }
  24. System.out.println("主串中共包含" + sum + "个字串");
  25. }
  26. }

【程序50】

题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文import java.io.File;

  1. import java.io.FileWriter;
  2. import java.util.Scanner;
  3. class Student {
  4. private int number = 0;
  5. private String name = "";
  6. private double[] a = new double[3];
  7. public double getAve() {
  8. return (a[0] + a[1] + a[2]) / 3;
  9. }
  10. public Student(int number, String name, double[] a) {
  11. super();
  12. this.number = number;
  13. this.name = name;
  14. this.a = a;
  15. }
  16. @Override
  17. public String toString() {
  18. return "学号:" + this.number + "\t姓名:" + this.name + "\r\n各科成绩:\r\n" + a[0] + "\t" + a[1] + "\t" + a[2] + "\r\n平均成绩:\r\n"
  19. \+ this.getAve();
  20. }
  21. }
  22. public class Demo50 {
  23. public static Student input() {
  24. Scanner s = new Scanner(System.in);
  25. System.out.println("请输入学号:");
  26. int num = s.nextInt();
  27. System.out.println("请输入姓名:");
  28. String name = s.next();
  29. System.out.println("请分别输入3门成绩:");
  30. double[] a = new double[3];
  31. for (int i = 0; i < 3; i++) {
  32. a[i] = s.nextDouble();
  33. }
  34. return new Student(num, name, a);
  35. }
  36. public static void main(String[] args) throws Exception {
  37. Student[] st = new Student[2];
  38. for (int i = 0; i < st.length; i++) {
  39. st[i] = input();
  40. }
  41. File f = new File("d:" + File.separator + "123.txt");
  42. FileWriter output = new FileWriter(f);
  43. for (int i = 0; i < st.length; i++) {
  44. output.write(st[i].toString() + "\r\n");
  45. output.write("\r\n");
  46. }
  47. output.close();
  48. }
  49. }

好了,今天就到这儿吧,我是冰河,我们下期见!!

发表评论

表情:
评论列表 (有 0 条评论,416人围观)

还没有评论,来说两句吧...

相关阅读

    相关 字节……

    应广大读者要求,今天开更一些大厂的面经和相关的面试干货,下面这份新鲜的字节跳动春招面经 带给大家。 普本 / 软件工程 / 2年经验 2021春招 / base北京 / 字

    相关 字节

    今天给大家介绍一个身边的牛逼号主:findyi,他工作四年就担任360技术总监,年薪过百万。目前他在网易做部门负责人,他的公号输出计算机学习路线、职场晋升等干货,不少洋哥的读者

    相关 JAVA经典算法50

    【程序1】   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程