求一个数的阶层,这个数很大,例如100、1000的,
这样一个int就放不下,那怎么做才能算出这个数的阶层呢?
我明白用数组,就是想不出来怎么写!
那位大哥指点一下,最好有代码!
这样一个int就放不下,那怎么做才能算出这个数的阶层呢?
我明白用数组,就是想不出来怎么写!
那位大哥指点一下,最好有代码!
解决方案 »
- peek用法?
- Java能调用火狐浏览器模块么?
- httpClient 100分紧急求助
- List array = new ArrayList();
- 各位大侠:40分求解一个小问题,望各位救我,十分感谢!
- 谁有《最新java2核心技术》书中的代码? 收到给分
- TB里无法打开APPLET
- 各位大侠,能告诉我怎么down某个package呢?例如 org.w3c.dom
- 有谁知道哪里有生成RTF文档的Java组件啊,小弟急用,分不够可在加
- java通过socket发送多个文件,c++接收
- 惊天错误:第一次知道JAVA编译器居然出错,大家看看错误详细提示信息:
- jbuilder2005使用泛型报错.
{
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;
}
}
* 文件Test.java
*
*/
public class Test
{
/**
* 大数加法
*/
public String add(String a,String b)
{
String big=a.length()>b.length()?a:b;
String small=a.length()>b.length()?b:a;
int dot=0; //进位
StringBuffer result=new StringBuffer("");
char[] cBig=((new StringBuffer(big)).reverse()).toString().toCharArray();
char[] cSmall=((new StringBuffer(small)).reverse()).toString().toCharArray();
/*
* 计算最小的一个与大的一个小的部分的和
*/
for(int i=0;i<cSmall.length;i++)
{
int temp=Integer.parseInt(String.valueOf(cBig[i]))+Integer.parseInt(String.valueOf(cSmall[i]))+dot;
dot=temp/10;
result.append(temp%10);
}
/*
* 对大的一个进行计算
*/
for(int j=cSmall.length;j<cBig.length;j++)
{
int temp2=Integer.parseInt(String.valueOf(cBig[j]))+dot;
dot=temp2/10;
result.append(temp2);
}
/*
* 结果的位数超过最大的时候进行进位
*/
if(dot>0)
{
result.append(dot);
}
result=result.reverse();
return result.toString();
}
/**
* 大数乘法
*/
public String multiply(String a,String b)
{
char[] cA=((new StringBuffer(a)).reverse()).toString().toCharArray();
char[] cB=((new StringBuffer(b)).reverse()).toString().toCharArray();
String result="0";
for(int i=0;i<cA.length;i++)
{
StringBuffer tempSingle=new StringBuffer("");
/*
* 一个数的每一位数乘以另外一个数
*/
int dot=0; //进位
for(int j=0;j<cB.length;j++)
{
int temp=Integer.parseInt(String.valueOf(cA[i]))*Integer.parseInt(String.valueOf(cB[j]))+dot;
tempSingle.append(temp%10);
dot=temp/10;
}
/*
* 处理进位
*/
if(dot>0)
{
tempSingle.append(dot);
}
tempSingle=tempSingle.reverse();
for(int k=0;k<i;k++)
{
tempSingle.append("0");
}
/*
* 把每一个数乘以另一个数的结果调用加法方法相加起来
*/
result=add(result,tempSingle.toString());
}
return result;
}
/**
* 求n的阶乘
*/
public String factor(String a)
{
String n="1";
for(int i=1;i<=Integer.parseInt(a);i++)
{
n=multiply(n,String.valueOf(i));
}
return n;
}
/**
* 主函数
*/
public static void main(String[] args)
{
Test test=new Test();
System.out.println(test.factor("100"));
}
}
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]);
}
剛學JAVA時寫的
沒經驗寫的不好
* @param args
*/
public static void main(String[] args) {
Factorial f = new Factorial();
BigInteger i = new BigInteger("100");
System.out.println("factorial of " + i + " is: " + f.factorial(i));
}
BigInteger factorial(BigInteger i){
if (i.compareTo(BigInteger.ONE)==0)
return BigInteger.ONE;
else
return factorial(i.subtract(BigInteger.ONE)).multiply(i);
}}
{
if (n < 0)
throw new IllegalArgumentException("n < 0");
BigInteger fact = BigInteger.ONE;
for (int i = n; i > 1; i--)
{
fact = fact.multiply(new BigInteger(Integer.toString(i)));
}
return fact;
}
所以用int
剛才臨時把9改成100所以可能結果是負數或者報錯
把int型改下應該可以
初學時寫的代碼
應該容易看懂
只不過還是沒達到樓主要求
不是用數組做的
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));
}
}
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));
}
}
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;
}}
// 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过来就都乱了
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);
}
}
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;
}}
这个是对的。。
大数阶乘的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:不是本人自己写的,转载网上朋友的!效率很高的,希望对楼主有所帮助。