经典递归
用递归法实现 从高位到低位的输出(借鉴 树的递归遍历)
#include<iostream>
using namespace std;
void f(int n)
{
if(n<10)
{
cout<<n;return;
}
else
{
f(n/10);
}
cout<<n%10;
}
int main()
{
int x;
while(cin>>x,x)
{
f(x);
}
return 0;
}
-----把 10换成 m 就是把10进制的 N转化为 M进制的数 如下--------
#include<iostream>
using namespace std;
void f(int n,int m)
{
if(n<m)
{
cout<<n;return;
}
else
{
f(n/m,m)
}
cout<<n%m;
}
int main()
{
int x,y;
while(cin>>x>>y,x,y)
{
f(x,y);
}
return 0;
}
---------------------------------------------------
把一个10进制数 表示成 2的幂次方形式 如 137=2^7+2^3+2^0
#include<iostream>
using namespace std;
void f(int n,int r)
{
if(n ==1)
{
cout<<"2("<<r<<")";return ;
}
else
{
f(n/2,r+1);
if(n%2==1)
cout<<"+2("<<r<<")";
}
}
int main( )
{
int x;
while(cin>>x,x)
{
f(x,0);
}
return 0;
}
-------------------------------------------------------------------
表示成 如 137=2(2(2)+2+2(0))+2(2+2( 0 ) )+2( 0 )
#include<iostream>
using namespace std;
void f(int n,int r)
{
if(n ==1)
{
if(r<=2&&r!=1)
cout<<"2("<<r<<")";
else if(r>2)
{
cout<<"2("; f(r,0); cout<<")";
}
if(r==1)
cout<<"2";
return ;
}
else
{
f(n/2,r+1);
if(n%2==1)
{
if(r<=2&&r!=1)
cout<<"+2("<<r<<")";
else if(r>2)
{ cout<<"+2("; f(r,0); cout<<")"; }
if(r==1)
cout<<"+2";
}
}
}
int main()
{
int x;
while(cin>>x,x)
{
f(x,0);
}
return 0;
}
----------------------------------
用 switch 代替 if else 看起来清晰简洁
#include<iostream>
using namespace std;
void f(int x,int r)
{
if(x==1)
{
switch(r)
{
case 0: cout<<"2(0)";break;
case 1: cout<<"2";break;
case 2: cout<<"2(2)";break;
default:cout<<"2(";f(r,0);cout<<")";
}
return ;
}
else
{
f(x/2,r+1);
}
if(x%2==1)
{
switch (r)
{
case 0:cout<<"+2(0)";break;
case 1: cout<<"+2";break;
case 2: cout<<"+2(2)";break;
default:cout<<"+2(";f(r,0);cout<<")";
}
}
}
int main()
{int n;
while(cin>>n,n)
{
f(n,0);
}
return 0;
}
--------------------------------------------------------------------------
找出n个自然数(1,2,3,4,5,6.。。。。,n)中 取 r 个数的组合。
#include<iostream>
using namespace std;
int a[100];
void f(int n,int r)
{
int i,j;
for(i=n;i>=r;i--)
{
a[r]=i;
if(r>1)
f(i-1,r-1);
else
{
for(j=a[0];j>0;j--)
cout<<a[j]<<" ";
cout<<endl;
}
}
}
int main()
{
int n,r;
cin>>n>>r;
a[0]=r;
f(n,r);
return 0;
}
------------------------------------------------------
整数的划分问题
f(n,n)表示 整数n 不超过n的 划分;
f(n,n)=1+f(n,n-1)
f(n,m)=f(n,m-1)+f(n-m,m)
限制条件 : f(1,n)=1 f(n,1)=1
#include<iostream>
using namespace std;
int f(int n,int m)
{
if(n<1||m<1){cout<<"error";return 0;}
int a=m,b=n;
if(n==1||m==1)return 1;
if(n<m)
return f(n,n);
if(n==m)
{
return (1+f(n,n-1));
}
if(n>m)
return (f(n,m-1)+f(n-m,m));
}
int main()
{
int n,m;
while(cin>>n>>m,n,m)
{
//cout<<<<endl;
cout<<f(n,m);
}
return 0;
}
用递归法求字符串是否为回文串
#include<iostream>
using namespace std;
bool myFunc(char s[], int firstIndex, int lastIndex)
{
if(firstIndex >= lastIndex)
return true;
else
{
if(s[firstIndex] != s[lastIndex])
return false;
else
return myFunc(s, firstIndex+1, lastIndex-1);
}
}
int main()
{
char s[100];
cout << "Input a string: ";
while(cin>>s)
{
int firstIndex = 0;
int lastIndex = strlen(s)-1;
bool result = myFunc(s, firstIndex, lastIndex);
if(result)
cout << s << " is a palindrome!" << endl;
else
cout << s << " is not a palindrome!" << endl;
}
return 0;
}
------------------------------------------------
递归实现 全排列
#include<stdio.h>
#define SWAP(a,b,t) ((t)=(a),(a)=(b),(b)=(t))
void perm(char *list,int i,int n)
{
int j;
int temp;
if(i == n) //一个全排列已经完成,打印整个排列
{
for(j = 0; j <=n; j++)
{
printf("%c",list[j]);
}
printf(" ");
}
else
{
for(j = i; j <= n; j++)
{
SWAP(list[i],list[j],temp); //依次将1,2,...n个元素放在第一个位置
perm(list,i+1,n); //递归进行全排列
SWAP(list[i],list[j],temp); //由于第一个SWAP将第j个元素放到第一个位置进行了
//递归全排列,这里就将列表还原,以进行第2次递归
}
}
}
int main()
{
char list[4] = {'0','1','2','3'};
perm(list,0,3);
return 0;
}