码迷,mamicode.com
首页 > 其他好文 > 详细

不使用局部变量和for循环或其它循环打印出如m=19,n=2結果为2 4 8 16 16 8 4 2形式的串

时间:2015-03-05 12:39:59      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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

不使用局部变量和for循环或其它循环打印出如m=19,n=2結果为2 4 8 16 16 8 4 2形式的串

标签:

原文地址:http://www.cnblogs.com/haitunsw/p/4315324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!