java代码实现前两个数相加=第三个,只使用一次递归的谁能帮我实现
public class test {
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
int n=10;
int[] a=new int[2];
int temp=0;
for(int i=0;i<n;i++){
//temp=getint(i);
//System.out.print(getint(i)+" ");
//System.out.print(getint1(i,a)+" ");
System.out.print(getint2(i,temp)+" ");
}
}
/**
* 使用递归
* @param n
* @return
*/
public static int getint(int n){
if(n==0){
return 0;
}else if(n==1){
return 1;
}else{
return getint(n-1)+getint(n-2);
}
}
/**
* 只使用一次递归
* @param n
* @return
*/
public static int getint2(int n,int temp){
if(n==0){
temp=0;
return 0;
}else if(n==1){
temp=1;
return 1;
}else{
temp=getint(n-2);
return temp+getint(n-2);
}
}
/**
* 使用数组的方式
* @param n
* @param a
* @return
*/
public static int getint1(int n,int[] a){
int tmp=0;
if(n==0){
a[0]=0;
return 0;
}else if(n==1){
a[1]=1;
return 1;
}else{
tmp=a[1];
a[1]+=a[0];
a[0]=tmp;
return a[1];
}
}
}