递归(网上搜的一些笔试题)

```public class Cs
{
public int times;
public int score;
public int[] loops;
public int count=0;

public static void main(String[] args)
{
Cs cs=new Cs(10,90);
cs.loop(10);
System.out.println(cs.count);
}

public Cs(int times,int score)
{
this.times=times;
this.score=score;
loops=new int[times];
}

public void loop(int cur)
{
if(cur==0)
{
if(score!=0)
return;
count++;
return;
}
cur--;
for(int i=10;i>0;i--)
{
loops[cur]=i;
score-=i;	//模拟嵌套for循环
loop(cur);
score+=i;   //状态恢复
}
}
}
```

```public class Cs
{
public int n;
public int m;
public int[] result;
public int count=0;

public static void main(String[] args)
{
Cs cs=new Cs(10,50);
cs.loop();
System.out.println(cs.count);
}

public Cs(int n,int m)
{
this.n=n;
this.m=m;
result=new int[n];
}

public void loop()
{
int[] loops=new int[n];
result=new int[n];
for(int i=0;i<n;i++)
loops[i]=i+1;
loop(n,loops);
}

private void loop(int cur,int[] _loop)
{
if(m<=0||_loop.length==0)
{
if(m!=0)
return;
count++;
for(int i=0;i<result.length;i++)
System.out.print(result[i]+",");
System.out.println("");
return;
}
cur--;
for(int i=0;i<_loop.length;i++)
{
result[cur]=_loop[i];
m-=_loop[i];
//等到最终结果需要不重复，可以按元素大小排序来得到下一步_loop
int[] _loop_=new int[cur];
for(int j=i+1;j<_loop.length;j++)
_loop_[j-i-1]=_loop[j];
loop(cur,_loop_);
m+=_loop[i];  //状态恢复
}
}
}

```

```bool fun( int a[], int n )
{
if( n= =1 )
return true;
if( n= =2 )
return a[n-1] >= a[n-2];
return fun( a,n-1) && ( a[n-1] >= a[n-2] );
}```

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊