趣味Java算法题(附答案)

2022-12-07,,,

【程序1】   

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

//这是一个菲波拉契数列问题

public class lianxi01 {

public static void main(String[] args) {

System.out.println("第1个月的兔子对数:    1");

System.out.println("第2个月的兔子对数:    1");

int f1 = 1, f2 = 1, f, M=24;

     for(int i=3; i<=M; i++) {

      f = f2;

      f2 = f1 + f2;

      f1 = f;

      System.out.println("第" + i +"个月的兔子对数: "+f2);

         }

}

}

【程序2】   

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

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

public class lianxi02 {

public static void main(String[] args) {

    int count = 0;

    for(int i=101; i<200; i+=2) {

     boolean b = false;

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

     {

        if(i % j == 0) { b = false; break; } 

         else           { b = true; }

     }

        if(b == true) {count ++;System.out.println(i );}

                                  }

    System.out.println( "素数个数是: " + count);

}

}

【程序3】   

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

public class lianxi03 {

public static void main(String[] args) {

     int b1, b2, b3; 

     for(int m=101; m<1000; m++) { 

      b3 = m / 100;

      b2 = m % 100 / 10;

      b1 = m %    10;

      if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) {

      System.out.println(m+"是一个水仙花数"); }

     }

}

}   

【程序4】   

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

import java.util.*;

public class lianxi07 {

public static void main(String[] args) {

int digital = 0;

int character = 0;

int other = 0;

int blank = 0;

     char[] ch = null;

     Scanner sc = new Scanner(System.in);

     String s = sc.nextLine();

     ch = s.toCharArray();

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

      if(ch >= '0' && ch <= '9') {

       digital ++;

      } else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') {

       character ++;

      } else if(ch == ' ') {

       blank ++;

      } else {

       other ++;

      }

      }

     System.out.println("数字个数: " + digital);

     System.out.println("英文字母个数: " + character);

     System.out.println("空格个数: " + blank);

     System.out.println("其它字符个数:" + other );

}

}

【程序5】   

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

public class lianxi10 {

public static void main(String[] args) {

      double h = 100,s = 100;

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

      s = s + h;

      h = h / 2;

     }

     System.out.println("经过路程:" + s);

     System.out.println("反弹高度:" + h / 2);

}

【程序6】   

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

import java.util.*;

public class lianxi15 {

public static void main(String[] args) {

     input fnc = new input();

     int x=0, y=0, z=0;

     System.out.print("输入第一个数字:");

      x = fnc.input();

     System.out.print("输入第二个数字:");

      y = fnc.input();

     System.out.print("输入第三个数字:");

      z = fnc.input();   

    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;

     }

    System.out.println( "三个数字由小到大排列为: "+x + " " + y + " " + z);

}

}

class input{

public int input() {

     int value = 0;

     Scanner s = new Scanner(System.in);

     value = s.nextInt();

     return value;

}

【程序7】

题目:输出9*9口诀。     

public class lianxi16 {

public static void main(String[] args) {

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

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

       System.out.print(j + "*" + i + "=" + j*i + "    " );

         if(j*i<10){System.out.print(" ");}

}

          System.out.println();

     }

}

【程序8】   

题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个     第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下     的一半零一个。到第10天早上想再吃时,见仅仅剩下一个桃子了。求第一天共摘了多少。   

public class lianxi17 {

public static void main(String[] args) {

     int x = 1;

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

      x = (x+1)*2;

     }

     System.out.println("猴子第一天摘了 " + x + " 个桃子");

}

}

【程序9】   

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

     *   

   ***   

 *****   

*******   

 *****   

   ***   

    *   

public class lianxi19 {

public static void main(String[] args) {

    int H = 7, W = 7;//高和宽必须是相等的奇数

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

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

      System.out.print(" ");

     }

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

      System.out.print('*');

     }

     System.out.println();

    }

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

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

      System.out.print(" ");

     }

     for(int k=1; k<=W-2*i; k++) {

      System.out.print('*');

     }

     System.out.println();

    }

}

}

【程序10】   

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

public class lianxi20 {

public static void main(String[] args) {

    int x = 2, y = 1, t;

    double sum = 0;

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

     sum = sum + (double)x / y;

     t = y;

     y = x;

     x = y + t;

     }

System.out.println("前20项相加之和是: " + sum);

}

}

【程序11】   

题目:一个5位数,推断它是不是回文数。即12321是回文数,个位与万位同样,十位与千位同样。   

import java.util.*;

public class lianxi25 {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);

    int a;

    do{

     System.out.print("请输入一个5位正整数:");

      a = s.nextInt();

      }while(a<10000||a>99999);

     String ss =String.valueOf(a);

     char[] ch = ss.toCharArray();

     if(ch[0]==ch[4]&&ch[1]==ch[3]){

     System.out.println("这是一个回文数");}

     else {System.out.println("这不是一个回文数");}

    }

    }

//这个更好,不限位数

import java.util.*;

public class lianxi25a {

public static void main(String[] args) {

   Scanner s = new Scanner(System.in);

   boolean is =true;

   System.out.print("请输入一个正整数:");

   long a = s.nextLong();

   String ss = Long.toString(a);

   char[] ch = ss.toCharArray();

   int j=ch.length;

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

   if(ch[i]!=ch[j-i-1]){is=false;}

   }

   if(is==true){System.out.println("这是一个回文数");}

     else {System.out.println("这不是一个回文数");}

    }

   }

【程序12】   

题目:对10个数进行排序   

import java.util.*;

public class lianxi28 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

   int[] a = new int[10];

   System.out.println("请输入10个整数:");

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

    a[i] = s.nextInt();

   }

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

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

     if(a[i] > a[j]) {

      int t = a[i];

      a[i] = a[j];

      a[j] = t;

     }

    }

   }

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

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

   }

}

}

趣味Java算法题(附答案)的相关教程结束。

《趣味Java算法题(附答案).doc》

下载本文的Word格式文档,以方便收藏与打印。