java计算1/2+2!/3+3!/4+5!/6+...+(n-1)!/n直到最后一项大于1000
4个回答
展开全部
当n = 9的时候满足条件
public class Du {
public static void main(String[] args) {
boolean isNFound = false;
int fac = 1;
int i = 1;
double sum = 0;
double num=0;
while (!isNFound) {
fac *= i;
num = (double) fac / (i + 1);
isNFound = num > 1000;
if (!isNFound) {
sum += fac / (i + 1);
}
i++;
}
System.out.println("The max n is: " + i);
System.out.println((i-1) + "!/" + (i) + "=" + num);
System.out.println("1/2 + 2!/3 + 3!/4 + ... + " + (i-1) + "!/" + i + " = " + sum);
}
}
--------------
The max n is: 9
8!/9=4480.0
1/2 + 2!/3 + 3!/4 + ... + 8!/9 = 757.0
public class Du {
public static void main(String[] args) {
boolean isNFound = false;
int fac = 1;
int i = 1;
double sum = 0;
double num=0;
while (!isNFound) {
fac *= i;
num = (double) fac / (i + 1);
isNFound = num > 1000;
if (!isNFound) {
sum += fac / (i + 1);
}
i++;
}
System.out.println("The max n is: " + i);
System.out.println((i-1) + "!/" + (i) + "=" + num);
System.out.println("1/2 + 2!/3 + 3!/4 + ... + " + (i-1) + "!/" + i + " = " + sum);
}
}
--------------
The max n is: 9
8!/9=4480.0
1/2 + 2!/3 + 3!/4 + ... + 8!/9 = 757.0
展开全部
private static double factorial(int n){
if(n==1){
return 1;
}
return n * factorial(n-1);
}
public static double calculate(int n){
if(n==1){
return 1;
}
double nFactorialValue = factorial(n);
double tmpValue = nFactorialValue / n;
return tmpValue + calculate(n-1);
}
public static void main(String[] args){
System.out.println( calculate(1000) );
}
if(n==1){
return 1;
}
return n * factorial(n-1);
}
public static double calculate(int n){
if(n==1){
return 1;
}
double nFactorialValue = factorial(n);
double tmpValue = nFactorialValue / n;
return tmpValue + calculate(n-1);
}
public static void main(String[] args){
System.out.println( calculate(1000) );
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public int getResult(){
int sum = 0;
int k=1;
for(int i=1;i<1000;i++){
for(int m=0;m<i;m++){
k=k*(m+1);
}
k=k/i;
sum=sum+k;
if(k>1000){
System.out.println(sum-k);
System.out.println(k);
System.out.println(i);
break;
}
k=1;
}
return sum;
}
int sum = 0;
int k=1;
for(int i=1;i<1000;i++){
for(int m=0;m<i;m++){
k=k*(m+1);
}
k=k/i;
sum=sum+k;
if(k>1000){
System.out.println(sum-k);
System.out.println(k);
System.out.println(i);
break;
}
k=1;
}
return sum;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
恩恩,不多说,简单的递归函数调用,你懂得.......
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询