JAVA经典算法题 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... public class exp2{ public static void main(String args[]){ int i=0; for(i=1;i<=20;i++) System.out.println(f(i)); } publ
题目 /*You are a professional robber planning to rob houses along a street. * Each house has a certain amount of money stashed, * the only constraint stopping you from robbing each of them is that * adjacent houses have security system connected and *
1.给数组做反序 public class Ak01 { public static void main(String[] args) { int[] a = new int[]{22,48,41,2,7,9}; int start=0; int end=a.length-1; int size = a.length; for(int i = 0;i<size/2;i++) { int temp; temp = a[start]; a[start]=a[end]; a[end]=temp; st
题目:判断101-200之间有多少个素数,并输出所有素数. 思路:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数. 具体代码: public Vector exp(int first, int end) { Vector v = new Vector(); boolean b; for (int i = first; i <= end; i++) { b = true;// 假设是质数 for (int j = 2; j < i; j++)
12个人围成一圈,序号依次从1至12,从序号1开始顺时针依次数,数到7的人退出,下一个再依次从1开始数,求留下来的最后一个人的原始序号. public static void joseph(int[] array,int n) { for (int i = 0; i < array.length; i++) { array[i] = i+1; } // 计数器 int counter = 0; // 剩余人数 int leftCount = array.length; // 索引 int ind
1.下面输出结果是什么? public class Test { public static void main(String[] args) { Person person=new Person("张三"); change(person); System.out.println(person.name); } public static void change(Person person) { Person person2=new Person("李四"); pe