今天看到一个老问题,求阶乘的
以前学delphi的时候,曾经解过,不过现在学java了,就试了一下,呵呵,还行。
不过感觉自己代码写得太乱,对java函数理解的还不是很深。
希望有朋友能多指点,,,
public class Factorial {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int value[] = new int[100000];
int num;
int nowlength;
int temp;
String str = "";
value[0] = 1;
for(int i = 1; i <= 100000 - 1; i++)
value[i] = 0;
nowlength = 1;
num = 10000;
for(int i =1; i <= num; i++){
for(int j = 0; j <= nowlength - 1; j++ )
value[j] *= i;
if(nowlength > 1){
for(int j = 0; j <= nowlength - 2; j++ ){
value[j + 1] += value[j] / 10000;
value[j] = value[j] % 10000;
}
}
if(value[nowlength - 1] > 10000){
if(nowlength > 100000)
System.out.println("OverFlow");
value[nowlength] = value[nowlength - 1] / 10000;
value[nowlength - 1] = value[nowlength - 1] % 10000;
nowlength++;
}
}
str = "" + value[nowlength - 1];
for(int i = nowlength - 2; i >= 0; i--){
if(value[i] == 0)
str += "0000";
else if(value[i] < 10)
str += "000" + value[i];
else if(value[i] < 100 )
str += "00" + value[i];
else if(value[i] < 1000)
str += "0" + value[i];
else
str += "" + value[i];
}
System.out.println(str);
}
}