求N个数的阶层!!!!!!!!!!

lsh_li 2008-12-04 01:56:25
求一个数的阶层,这个数很大,例如100、1000的,
这样一个int就放不下,那怎么做才能算出这个数的阶层呢?
我明白用数组,就是想不出来怎么写!
那位大哥指点一下,最好有代码!
...全文
695 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
pussmug 2008-12-09
  • 打赏
  • 举报
回复
楼主大舌头。。。“阶乘”说成“阶层”。。。
anxingyu_1984 2008-12-09
  • 打赏
  • 举报
回复
BigInteger 类型应该可以
lulu0126 2008-12-08
  • 打赏
  • 举报
回复


/*
大数阶乘的java实现

*/
import java.math.BigInteger;
import java.util.*;
public class Factorial
{
protected static ArrayList table=new ArrayList();
static
{
table.add(BigInteger.valueOf(1));
}

/** Creates a new instance of factorial */
public static synchronized BigInteger factorial(int x)
{
if(x<0) throw new IllegalArgumentException("x must be non-negative.");
for(int size=table.size();size<=x;size++)
{
BigInteger lastfact=(BigInteger)table.get(size-1);
BigInteger nextfact=lastfact.multiply(BigInteger.valueOf(size));
table.add(nextfact);
}
return (BigInteger) table.get(x);
}

public static void main(String[] args)
{

System.out.println("100的阶乘:"+factorial(100));
System.out.println("1000的阶乘:"+factorial(1000));

}
}


PS:不是本人自己写的,转载网上朋友的!

效率很高的,希望对楼主有所帮助。
vincentjang 2008-12-08
  • 打赏
  • 举报
回复
29楼的不错
100的阶乘那么大。。
jingyan_126 2008-12-08
  • 打赏
  • 举报
回复
100!=93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
20!=2432902008176640000
jingyan_126 2008-12-08
  • 打赏
  • 举报
回复
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class jieCheng {

public static void main(String[] args) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个整数:");
try {
int a = Integer.parseInt(br.readLine());
System.out.println(a + "!=" + SuanFa(a));

} catch (NumberFormatException e) {
System.out.println("您输入的数有误!");
} catch (IOException e) {
System.out.println("您输入的数有误!");
}

}

public static BigInteger SuanFa(int b) {
BigInteger sum = new BigInteger("1");
for (; b > 0; b--) {
sum = sum.multiply(new BigInteger(new Integer(b).toString()));
}
return sum;
}

}




这个是对的。。
tigoal 2008-12-08
  • 打赏
  • 举报
回复
什么是阶层
zlyymxl 2008-12-08
  • 打赏
  • 举报
回复
递归实现
ssqtjffcu 2008-12-04
  • 打赏
  • 举报
回复

import java.math.BigInteger;

public class Test {
public static void main(String[] args) {
m(5);
}

public static void m(int n) {
BigInteger sum = new BigInteger("1");
for(; n>0 ;n--) {
sum = sum.multiply(new BigInteger("" + n));
}
System.out.println(sum);
}
}
CDMA1333 2008-12-04
  • 打赏
  • 举报
回复
去查下java.math.BigDecimal的API吧 就他可以实现了
RJCHEN1985 2008-12-04
  • 打赏
  • 举报
回复
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
int[] res = new int[3000];
final int limit = 1000;

res[1] = 1;
int max_now = 1;

for (int step = 1; step <= limit; step++) {
int temp = 0;
int now = max_now;
int zero;
for (zero = 1; zero <= now; zero++)
{
if (res[zero] != 0)
break;
}

for (int carry = zero - 1; carry <= now; carry++) {
res[carry] *= step;
res[carry] += temp;
temp = 0;
if (res[carry] >= 10) {
int carry_temp = carry;
temp = res[carry];
if (carry_temp <= max_now) {
res[carry_temp] = temp % 10;
temp /= 10;
carry_temp++;
}
if (carry_temp > max_now) {
while (temp >= 10) {
res[carry_temp] = temp % 10;
temp /= 10;
carry_temp++;
}
res[carry_temp] = temp;
temp = 0;
max_now = carry_temp;
}
}
}

}
for (int j = max_now; j > 0; j--) {
System.out.print(res[j]);
}
}
copy过来就都乱了
lsh_li 2008-12-04
  • 打赏
  • 举报
回复
BigInteger对它很陌生,谁给介绍一下呀?
pepsighost 2008-12-04
  • 打赏
  • 举报
回复


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class TestJC {

public static void main(String[] args) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个整数:");
try {
int a = Integer.parseInt(br.readLine());
System.out.println(a + "!=" + SuanFa(a));

} catch (NumberFormatException e) {
System.out.println("您输入的数有误!");
} catch (IOException e) {
System.out.println("您输入的数有误!");
}

}

public static BigInteger SuanFa(int b) {
BigInteger sum = new BigInteger("1");
for (; b > 0; b--) {
sum = sum.multiply(new BigInteger(new Integer(b).toString()));
}
return sum;
}

}



pjhdaye 2008-12-04
  • 打赏
  • 举报
回复
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigNumber {
private BigNumber() {
}
/**
* 提供精确的乘法运算。
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return (b1.multiply(b2)).doubleValue();
}
/**
* 计算Factorial阶乘!
* @param n 任意大于等于0的int
* @return n!的值
*/
public static BigInteger getFactorial(int n) {
if (n < 0) {
System.err.println("n必须大于等于0!");
return new BigInteger("-1");
} else if (n == 0) {
return new BigInteger("0");
}
//将数组换成字符串后构造BigInteger
BigInteger result = new BigInteger("1");
for (; n > 0; n--) {
//将数字n转换成字符串后,再构造一个BigInteger对象,与现有结果做乘法
result = result.multiply(new BigInteger(new Integer(n).toString()));
}
return result;
}

public static void main(String[] args) {

//计算阶乘,可以将n设得更大
int n = 100;
System.out.println("计算n的阶乘" + n + "! = " + BigNumber.getFactorial(n));
}
}
pjhdaye 2008-12-04
  • 打赏
  • 举报
回复
我的这个可以实现!
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigNumber {
private BigNumber() {
}
/**
* 提供精确的乘法运算。
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return (b1.multiply(b2)).doubleValue();
}
/**
* 计算Factorial阶乘!
* @param n 任意大于等于0的int
* @return n!的值
*/
public static BigInteger getFactorial(int n) {
if (n < 0) {
System.err.println("n必须大于等于0!");
return new BigInteger("-1");
} else if (n == 0) {
return new BigInteger("0");
}
//将数组换成字符串后构造BigInteger
BigInteger result = new BigInteger("1");
for (; n > 0; n--) {
//将数字n转换成字符串后,再构造一个BigInteger对象,与现有结果做乘法
result = result.multiply(new BigInteger(new Integer(n).toString()));
}
return result;
}

public static void main(String[] args) {

//计算阶乘,可以将n设得更大
int n = 100;
System.out.println("计算n的阶乘" + n + "! = " + BigNumber.getFactorial(n));
}
}
「已注销」 2008-12-04
  • 打赏
  • 举报
回复
忘了原來寫作業的時候只需要計算10一下的
所以用int
剛才臨時把9改成100所以可能結果是負數或者報錯
把int型改下應該可以
初學時寫的代碼
應該容易看懂
只不過還是沒達到樓主要求
不是用數組做的
一天十小时 2008-12-04
  • 打赏
  • 举报
回复
直接把返回值改成long型不就可以了,再大的值最后不还是要存起来吗
一天十小时 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mizukusa 的回复:]
引用 1 楼 waley_mei 的回复:
public class Jiecheng
{
public static void main(String args[])
{
Factorial f = new Factorial();
int i = 100;
System.out.println("factorial of " + i + " is: " + f.factorial(i));
}
}

class Factorial
{
int factorial(int i)
{
if (i == 0 || i == 1)
return 1;
else
return factorial(i - 1) * i;
}
}

你直接赋值给int型的肯定会超出int型…
[/Quote]
不会出错吧,只是结果会是负数
一天十小时 2008-12-04
  • 打赏
  • 举报
回复
大家写代码,尤其是比较复杂有点乱的,加个注解好不好,要不得浪费很多时间看
萧涵007 2008-12-04
  • 打赏
  • 举报
回复
BigInteger或者用数组实现
加载更多回复(14)

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧