求n的介乘 我想利用实参调用行参 用递归方法写个程序
一个类两个方法就可以了一个方法用命令行输出 n 另一个方法就是递归了
原码谁能帮我写个 一定要行参调用行参的那种 还得利用上递归方法 如下
long Factorial(int n)
{
long temp
if(n==1||n==0)
retunrn n;
else
temp= n*Factorial(n-1);
return temp;
}
一个类两个方法就可以了一个方法用命令行输出 n 另一个方法就是递归了
原码谁能帮我写个 一定要行参调用行参的那种 还得利用上递归方法 如下
long Factorial(int n)
{
long temp
if(n==1||n==0)
retunrn n;
else
temp= n*Factorial(n-1);
return temp;
}
{
long temp
if(n==1||n==0)
retunrn n;
else
temp= n*Factorial(n-1);
return temp;
}
中的(int n)是形参啊
}f(1,2); //1 2是实参另外,从你的问题“实参调用行参”我觉得你没理解什么是实参,什么是形参
这样做会快一些吗??
long Factorial(int n)
{
long temp=1;
for(int i=n;i>0;i--)
{
temp=i*temp;
}
return temp;
}
哪位高手能解答一下小弟的疑问??
retunrn n; // ??? 0! = 1;
Class M {
public static long Factorial(int n) {
return (n<2)?n:n*Factorial(n-1);
}
}// Run as:
System.out.println(M.Factorial(3));
public static double Factorial(int n)
{
double tmp = 1.0;
for(int i=1;i<=n;i++)
{
tmp *= i;
}
result += tmp;
if(n>=0)
{
n--;
if(n>=0)Factorial(n);
}
return result;
}
public static double Factorial(int n){
double temp=1;
if(n<2){
return temp;
}
else{
for(int i=n;i>0;i--){
temp=i*temp;
}
return temp;
}
}
import java.math.*;public class jiecheng
{ public static void main(String[] args)
{
jiecheng jc=new jiecheng();
for(long i=0;i<=100;i++)
{
System.out.println((int)i+"!="+jc.shown((int)i));//int
System.out.println(i+"!="+jc.shown1(i));//long
System.out.println(i+"!="+jc.shown2(i));//double
System.out.println(new BigDecimal(i).toString()+"!="+jc.shown3(new BigDecimal(i)));//BigDecimal
System.out.println();
}
}
//int
private int shown(int x)
{
if(x==1 || x==0)
return 1;
else
return x * shown(x-1);
}
//long
private long shown1(long x)
{
if(x==1 || x==0)
return 1;
else
return x * shown1(x-1);
}
//double
private double shown2(double x)
{
if(x==1 || x==0)
return 1;
else
return x * shown2(x-1);
}
//BigDecimal
private BigDecimal shown3(BigDecimal x)
{
if(x.equals(new BigDecimal(1)) || x.equals(new BigDecimal(0)))
return new BigDecimal(1);
else
return x.multiply(shown3(x.add(new BigDecimal(-1))));
}
}
我的意思是这样的如下:
书中的例子就是利用行参调用实参的
public class perfectnum
{
public static void main(strings args[])
{
int count=1;
for(int i=1;i<1000;i++)
{
if(isperfect(i))
{
system.out.print(i+string.valueof('\t'));
cout++;
}
if(cout%3==0)
system.out.println()
}
}
static blooean isperfect(int x)
{
int y=0;
for(int i=1:i<x;i++)
if(x%i==0)
y+=i;
if(y==x)
return ture;
else
return else;
}
中的if(isperfect(i)) i 是实参 static blooean isperfect(int x)中的 int x是行参
递归函数long Factorial(int n)中的 int n 能否利用上述static blooean isperfect(int x)中的 int x 形式当作行参 我的意思是这样的 大家看看啊
很感谢各位大哥呢
static boolean isperfect(int x)
{
int y=0;
for(int i=1:i<x;i++)
y +=(x%i==0)?0:i;
return y==x;
}