public static void Main(string[] args)
{
int m = 19;
int n = 2;
m = Recursionmult(m, n);
RecursionDivision(m, n);
//测试
m = 35;
n = 4;
Console.WriteLine("\nm=" + m + ",n=" + n);
m = Recursionmult(m, n);
RecursionDivision(m, n);
m = 40;
n = 5;
Console.WriteLine("\nm=" + m + ",n=" + n);
m = Recursionmult(m, n);
RecursionDivision(m, n);
m = 21;
n = 1;
Console.WriteLine("\nm=" + m + ",n=" + n);
m = Recursionmult(m, n);
RecursionDivision(m, n);
Console.Read();
}
public static int Recursionmult(int m, int n)
{
if (n <= m)
{
Console.WriteLine(n + " ");
n = n * 2;
return Recursionmult(m, n);
}
return n;
}
public static int RecursionDivision(int m, int n)
{
if (m > n)
{
m = m / 2;
Console.WriteLine(m + " ");
return RecursionDivision(m, n);
}
return n;
}
测试结果:
m=19,n=2
2 4 8 16 16 8 4 2
m=35,n=4
4 8 16 32 32 16 8 4
m=40,n=5
5 10 20 40 40 20 10 5
m=21,n=1
1 2 4 8 16 16 8 4 2 1