给大家做一个经典老题
[这个贴子最后由透明在 2002/12/23 07:08am 编辑]
刚做的,用一个数组来存放.
import java.awt.* ;
import javax.swing.* ;
import javax.swing.event.* ;
import java.text.* ;
import java.lang.* ;
public class Cheng
{
private long[] a ;
private long c ;
private int m = 0 ;
private int n, d;
private int y = 0 ;
private DecimalFormat myFormatter ;
/** Creates a new instance of Cheng */
public Cheng ()
{
myFormatter = new DecimalFormat("0000") ;
a = new long[30000] ;
a[0] = 1 ;
}
public void result()
{
String s ;
s = JOptionPane.showInputDialog (null,"please input number") ;
n = Integer.valueOf (s).intValue () ;
for (int i = 1 ; i <= n ; i++)
{
c = 0 ;
for (int j = y ; j <= m; j++)
{
a[j] = a[j] * i + c ;
c = a[j] / 10000 ;
if (a[j]%10000==0&&j==y)
{
y++ ;
a[j] = 0 ;
continue ;
}
a[j] = a[j] % 10000 ;
}
if (c > 0)
{
m++ ;
a[m] = c ;
}
}
System.out.print(a[m]);
for (int p = m -1 ; p >= y ; p--)
{
String output = myFormatter.format(a[p]) ;
if ( p % 30 == 1)
{
System.out.println();
}
System.out.print(output);
}
for (int i = 0 ; i < y ; i++)
{
if ( i % 30 == 1)
{
System.out.println();
}
System.out.print("0000");
}
System.out.println();
if (a[m] / 1000 > 0)
d = 4 ;
else if (a[m] /100 > 0)
d = 3 ;
else if (a[m] /10 > 0)
d = 2 ;
else
d = 1 ;
int w = m * 4 + d ;
System.out.println(n+"!为" +w +"位");
}
public static void main (String[] args)
{
Cheng ar = new Cheng() ;
ar.result () ;
System.exit(0) ;
}
}