## 阶乘问题！

2018年04月30日 ⁄ 综合 ⁄ 共 935字 ⁄ 字号 评论关闭

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);

}

}

【上篇】
【下篇】