【程序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算法题(附答案)的更多相关文章

  1. 一道java算法题分析

    最近在面试中遇到这样的一道算法题:       求100!的结果的各位数之和为多少?       如:5!=5*4*3*2*1=120,那么他们的和为1+2+0=3这道题不算难,不过倒是注意的细节也有 ...

  2. 面试-java算法题

    1.编写一个程序,输入n,求n!(用递归的方式实现). public static long fac(int n){ if(n<=0) return 0; else if(n==1) retur ...

  3. 精选30道Java笔试题附答案分析

    精选30道Java笔试题解答 都是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我一样参加各大IT校园招聘的同学们,纯考Java基础功底,老手们就不用进来了,免得笑 ...

  4. 2019最新整理JAVA面试题附答案

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  5. 2021精选 Java面试题附答案(一)

    1.什么是Java Java是一门面向对象的高级编程语言,不仅吸收了C++语言的各种优点,比如继承了C++语言面向对象的技术核心.还摒弃了C++里难以理解的多继承.指针等概念,,同时也增加了垃圾回收机 ...

  6. 某厂java算法题实现及改进【有n个人成一圈,顺序排号(编号为1到n),从第一个人开始报数1到3报数】

    一.第一种实现: 实现比较简单,直接贴现成的代码了,第一种实现: /** * 总人数 * * @param d */ private static void sortQuerry1(int d) { ...

  7. leetcode 一些算法题及答案

    1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...

  8. java 73题以及答案

    作者:乌枭原文:https://blog.csdn.net/qq_34039315/article/details/78549311 1.在java中守护线程和本地线程区别? java中的线程分为两种 ...

  9. 【JAVA算法题】职业抢劫

    题目 /*You are a professional robber planning to rob houses along a street. * Each house has a certain ...

随机推荐

  1. 为什么Lisp语言如此先进?(译文) - 阮一峰的网络日志

      为什么Lisp语言如此先进?(译文) - 阮一峰的网络日志 为什么Lisp语言如此先进?(译文)

  2. Extjs学习----------动态载入js文件(减轻浏览器的压力)

    动态载入js文件能够减轻浏览器的压力,本例使用了Ext.window.Window组件,该组件的学习地址:http://blog.csdn.net/z1137730824/article/detail ...

  3. 【C++版】Face Alignment at 3000 FPS by Regressing Local Binary Features源码下载

    下载地址: 本帖隐藏的内容 <ignore_js_op> face-alignment-in-3000fps-master.zip (794.42 KB, 下载次数: 1076) 该源码采 ...

  4. Chrome App远程控制

    現在google app連上chrome就能遠控了出了幾年了, 能用觸控控制mouse https://chrome.google.com/webstore/detail/chrome-remote- ...

  5. [C++]C++中的运行时类型检测

    Date:2014-1-3 Summary: 使用C++中的运行时类型检测.(文章重点在于记录本人的使用情况,并非深层讨论RTTI) Contents:写习惯C#的我,在C++依然存在哪些.NET的惯 ...

  6. Java使用Socket传输文件遇到的问题(转)

    1.写了一个socket传输文件的程序,发现传输过去文件有问题.找了一下午终于似乎找到了原因,记录下来警示一下: 接受文件的一端,向本地写文件之前使用Thread.sleep(time)休息一下就解决 ...

  7. HDU 4616 Game (搜索)、(树形dp)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4616 这道题目数据可能比较弱,搜索都可以AC,但是不敢写,哎…… 搜索AC代码: #include & ...

  8. PHP操作Mysql中间BLOB场

    1.MySQL在BLOB字段类型 BLOB场的类型用于存储二进制数据. MySQL在.BLOB它是一种类型的一系列.含有:TinyBlob.Blob.MediumBlob.LongBlob.大小上不同 ...

  9. loj1341(数学)

    传送门:Aladdin and the Flying Carpet 题意: 给出两个正整数1<=m<=n<=1e12.问N可以拆成多少对p*q,使得p和q中最小的不小于a,且p!=q ...

  10. POJ训练计划3080_Blue Jeans(串处理/暴力)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11542   Accepted: 4962 Descr ...