public class Test{
public static void main(String[] args) {
try{
int input = Integer.parseInt(args[0]);
String result = factorial(input);
System.out.println(result);
}catch( Exception e ){
System.out.println( e );
}
}
public static String factorial(int x) {
StringBuffer rs = new StringBuffer("1");
for (int i = 1; i <= x; i++) {
StringBuffer tmp_rs = new StringBuffer();
int highValue = 0;
for (int j = 0; j < rs.length(); j++) {
char cNum = rs.charAt(j);
int nNum = Integer.parseInt(String.valueOf(cNum));
int value = i * nNum + highValue;
int lowValue = value % 10;
highValue = value / 10;
tmp_rs.append(lowValue);
}
if (highValue != 0)
tmp_rs.append(highValue);
rs = tmp_rs;
}
return rs.reverse().toString();
}
}
public static void main(String[] args) {
try{
int input = Integer.parseInt(args[0]);
String result = factorial(input);
System.out.println(result);
}catch( Exception e ){
System.out.println( e );
}
}
public static String factorial(int x) {
StringBuffer rs = new StringBuffer("1");
for (int i = 1; i <= x; i++) {
StringBuffer tmp_rs = new StringBuffer();
int highValue = 0;
for (int j = 0; j < rs.length(); j++) {
char cNum = rs.charAt(j);
int nNum = Integer.parseInt(String.valueOf(cNum));
int value = i * nNum + highValue;
int lowValue = value % 10;
highValue = value / 10;
tmp_rs.append(lowValue);
}
if (highValue != 0)
tmp_rs.append(highValue);
rs = tmp_rs;
}
return rs.reverse().toString();
}
}
解决方案 »
- 谁有好一点的swing的教程,共享一下,thank you very much!
- JAVA求助(续) Socket编程问题 请各位高手帮助!!!
- javamail的sendmail问题553 Requested action not taken: address blocked
- 如何在JUnit4里面定义test method的执行顺序™
- >>>>>>>>关于抽象类<<<<<<<<<<<<<
- JAVA 与 Access直接连接出现问题,怎么办呀?
- 一个关于JSpinner显示小数位数的问题
- JAVA操作ZIP文件...急急急急急急急急急急急急
- jsp,javabean,servlet,struts有何关系?
- 一个jar命令的问题?
- 关于生成对象的值判别问题!
- 关于聊天程序中的问题,请诸高手帮帮忙
import java.util.*;public class Factorial4
{
public static void main(String[] args)
{
int input = Integer.parseInt(args[0]);
int result = factorial(input);
System.out.println(result);
}
public static BigInteger factorial(int x)
{
if(x < 0)
{
throw new IllegalArgumentException("x must be non-negative.");
}
else
{
BigInteger fact;
for(int i = 1; i < x; i++)
{
fact = (BigInteger.valueOf(i)).multiply((BigInteger.valueOf(1)));
}
return fact;
}
}
}
import java.util.*;public class Factorial4
{
public static void main(String[] args)
{
int input = Integer.parseInt(args[0]);
int result = factorial(input).intValue();
System.out.println(result);
}
public static BigInteger factorial(int x)
{
if(x < 0)
{
throw new IllegalArgumentException("x must be non-negative.");
}
else
{
BigInteger fact = BigInteger.valueOf(0);
for(int i = 1; i < x; i++)
{
fact = (BigInteger.valueOf(i)).multiply((BigInteger.valueOf(1)));
}
return fact;
}
}
}调试通过了。
环境: jdk1.4,Win2k Advance Server.
不过能介绍一个用BigInteger实现的方法吗!
你的程序调试是通过了,但并没有实现阶乘的功能..
只需稍加改动就可以了。
不过还是要十分的感谢大家.
结贴~~```import java.math.BigInteger;
import java.util.*;public class Factorial4 {
protected static ArrayList table = new ArrayList();
static {
table.add(BigInteger.valueOf(1));
}
public static void main(String[] args) {
int input = Integer.parseInt(args[0]);
BigInteger result = factorial(input);
System.out.println(result);
}
public static synchronized BigInteger factorial(int x) {
if(x < 0) throw new IllegalArgumentException("x must be non-negative.");
for(int i = 1; i <= x; i++) {
BigInteger last = (BigInteger)table.get(i-1);
BigInteger fact = last.multiply(BigInteger.valueOf(i));
table.add(fact);
}
return (BigInteger) table.get(x);
}
}