日期:2014-05-20  浏览次数:20776 次

请问怎么求n!的阶层
请问怎么求n!的阶层

------解决方案--------------------
public class Pr{

public static long passReturn(int i){

if(i == 1)

return i;

else
return i * passReturn(i - 1);

}

public static void main(String args[]){


int j = 10;
long k = passReturn(j);

System.out.println( "10的阶乘等于: " + k);

}


}
------解决方案--------------------
Private Sub Command1_Click()
Dim jc As Single
Dim i As Integer
Dim j As Integer
Dim n As Integer
n = InputBox( "shuru n= ")
jc = 1
For i = 1 To n
jc = jc * i
Next
Text1 = jc
End Sub

------解决方案--------------------
import java.io.*;
public class Factor
{
public static void main(String[] args) throws IOException
{
InputStreamReader ins=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ins);
System.out.print( "请输入您希望计算阶乘数的整数: ");
String str=br.readLine();
int m=Integer.parseInt(str);//将从键盘输入的字符串转换为数
m=jiecheng(m);//调用阶乘计算函数
System.out.println( "您输入的数的阶乘是: "+m);
}
static int jiecheng(int n)
{
if(n-1> 0)
{
n*=jiecheng(n-1);
}
return n;
}
}

------解决方案--------------------
import java.io.IOException;

public class ps {

public static void main(String[] args) throws IOException {
int i, j = 1, n;
System.out.println( "输入一个数: ");
n = (int) System.in.read();
n -= 48;
for (i = 1; i <= n; i++) {
j *= i;

}
System.out.println( "j = " + j);
}
}
------解决方案--------------------
Private Sub Command1_Click()
Dim jc As Single
Dim i As Integer
Dim j As Integer
Dim n As Integer
n = InputBox( "shuru n= ")
jc = 1
For i = 1 To n
jc = jc * i
Next
Text1 = jc
End Sub
----------------------------------
VB的都来了啊~
呵呵~~

不过大家都没有考虑溢出啊~
如果是100!呢?
------解决方案--------------------
用递归的确的考虑溢出啊。。。。
来个非递归的
private int factorialByLoop(int n)
{
int a = 1;
int b = 1;
while (b <=n)
{
a=a*b;
b++;
}
return a;
}
------解决方案--------------------
class A
{
public:
A(int k);
int itsnumreturn(){return itsnum;}
privte:
int *itsnum;
};
A::A(int k)
{
for(i=1;i <=k;i++)
k=k*i;
itsnum=k;
};

void main()
{ int n;
cout < < "please input the number " < <endl;
cin < < n;
A a(n);
cout < < "the number is " < <a.itsnumreturn() < <endl;
}