第一题:
public class Hailstone {
/* public static int Hailstone(int x){
arr[0]=x;
}*/
public static int hailstoneLength(int n){
for(int i=1;;i++) {
if (n == 1) {
return i;
} else if (n % 2 == 0) {
n /= 2;
} else {
n = n * 3 + 1;
}
}
}
public static boolean isLongSeq(int n){
if(hailstoneLength(n)>n) return true;
else return false;
}
public static double propLong(int n){
int s=0;
for(int i=1;i<=n;i++){
if(isLongSeq(i)==true) s++;
}
return (s*1.0)/n;
}
}
本文介绍了一个名为Hailstone的Java类,包含计算Hailstone序列长度、判断序列是否为长序列以及计算长序列比例的方法。主要关注递归算法的实现和性能影响。
944

被折叠的 条评论
为什么被折叠?



