编写一个Java程序计算1!+2!+.....+10!的和
展开全部
import java.math.*;
import java.io.*;
class fac
{
public static void main(String args[])
{ BigInteger n1=new BigInteger("0"),
n2=new BigInteger("1"),
result=null;
for(int i=1;i!=11;i++)
{
for(int j=1;j!=i+1;j++)
n2=n2.multiply(new BigInteger(String.valueOf(j)));
n1=n1.add(n2);n2=new BigInteger("1");
}
System.out.println(n1.toString());
}
}
import java.io.*;
class fac
{
public static void main(String args[])
{ BigInteger n1=new BigInteger("0"),
n2=new BigInteger("1"),
result=null;
for(int i=1;i!=11;i++)
{
for(int j=1;j!=i+1;j++)
n2=n2.multiply(new BigInteger(String.valueOf(j)));
n1=n1.add(n2);n2=new BigInteger("1");
}
System.out.println(n1.toString());
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
------------------循环算法-------------------------
public class text {
public static void main(String args[]) {
text t = new text();
System.out.print(t.sum());
}
public int sum(){
int a = 0;
for(int i=1;i<=10;i++){
int b = 1;
for (int j = 1; j <=i; j++) {
b = b*j;
}
a=a+b;
}
return a;
}
}
----------------递归算------------------------------------------
import java.io.*;
public class jiecheng
{
public static long fac(int n)
{long res=1;
for(int k=2;k<=n;k++)
res*=k;
return res;}
public static void main(String args[]) throws IOException
{long sum=0;int i=1,n=20;
for(i=1;i<=n;i++)
sum+=fac(i);
System.out.println("前"+n+"项阶乘的和为:"+sum);
}
}
public class text {
public static void main(String args[]) {
text t = new text();
System.out.print(t.sum());
}
public int sum(){
int a = 0;
for(int i=1;i<=10;i++){
int b = 1;
for (int j = 1; j <=i; j++) {
b = b*j;
}
a=a+b;
}
return a;
}
}
----------------递归算------------------------------------------
import java.io.*;
public class jiecheng
{
public static long fac(int n)
{long res=1;
for(int k=2;k<=n;k++)
res*=k;
return res;}
public static void main(String args[]) throws IOException
{long sum=0;int i=1,n=20;
for(i=1;i<=n;i++)
sum+=fac(i);
System.out.println("前"+n+"项阶乘的和为:"+sum);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询