斐波那契数列
规律:
1 1 2 3 5 8 13…….
不难发现前面两个数相加等于后面一个数,比如:2=1+1;3=2+1;…
public class Demo4 {
/**找规律,求数组最后一个数的和
* 1 1 2 3 5 8 13
*/
public static void main(String[] args) {
System.out.println(fun(8));
}
public static int fun(int x){
if(x==1||x==2){
return 1;
}else{
return fun(x-2)+fun(x-1);
}
}
}
计算1000的阶乘的所有的零
1000的阶乘数太大,int根本装不下,所以用BigInteger
public class Demo5_step {
/**
* 计算1000的阶乘的所有的零
* @param args
*/
public static void main(String[] args) {
BigInteger bi1=new BigInteger("1");
for(int i=2;i<=1000;i++){
BigInteger bi2=new BigInteger(i+"");
bi1=bi1.multiply(bi2);
}
String str=bi1.toString();
int count=0;
for(int i=0;i<str.length();i++){
if('0'==str.charAt(i)){
count++;
}
}
System.out.println(count);
}
}