1.递归的方法实现冒泡法
2. 用递归的方法实现下面推理逻辑,求第N位的值
1,1,2,3,5,8,13…….N
3. 用递归的方法实现下面推理逻辑,当最后一位的值是N是,求下面表达式的值
1-2+3-4+5-6+7……N
4.具体的实现附实现代码
1. 递归的方法实现冒泡法
2. 用递归的方法实现下面推理逻辑,求第N位的值。
1,1,2,3,5,8,13…….N
3. 用递归的方法实现下面推理逻辑,当最后一位的值是N是,求下面表达式的值
1-2+3-4+5-6+7……N
4.下面是具体的实现:
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("测试方法1");
string[] strs = new string[] {"A","B","C","D","E" };
Console.WriteLine("升序排序前的数组:");
Print<string>(strs);
Console.WriteLine("升序排序后的数组:");
string[] result = Menthon1<string>(strs, 0, 1);
Print<string>(result);
Console.WriteLine("再降序排序升序排序后的数组:");
result = Menthon1<string>(result, 0, 0);
Print<string>(result);
Console.WriteLine("测试方法2");
Console.WriteLine("求第6位的值:");
int result1 = Menthon2(6,1,1);
Console.WriteLine(result1);
Console.WriteLine("测试方法3");
Console.WriteLine("当n为5时表达式的值为:");
int result2 = Menthon3(5,0);
Console.WriteLine(result2);
Console.Read();
}
/// <summary>
/// 显示数组的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="values"></param>
static void Print<T>(T[] values)
{
for (int i = 0; i < values.Length;i++ )
{
Console.WriteLine(values[i].ToString());
}
}
/// <summary>
/// 递归的方法实现冒泡法排序
/// </summary>
/// <typeparam name="T">排序数组的类型</typeparam>
/// <param name="values">排序的数组</param>
/// <param name="start">开始的索引号</param>
/// <param name="type">排序的类型,1为升序,0为降序</param>
/// <returns></returns>
static T[] Menthon1<T>(T[] values,int start,int type)
{
T temp;
for (int i = start + 1; i < values.Length;i++ )
{
if (type.Equals(1))
{
if(values[i].ToString().CompareTo(values[start].ToString()).Equals(1))
{
temp= values[start];
values[start] = values[i];
values[i] = temp;
}
}
else
{
if (values[i].ToString().CompareTo(values[start].ToString()).Equals(-1))
{
temp = values[start];
values[start] = values[i];
values[i] = temp;
}
}
}
if(values.Length-1>start)
{
values = Menthon1<T>(values,start+1,type);
}
return values;
}
/// <summary>
/// 1,1,2,3,5,8,13n求N的值
/// </summary>
/// <param name="n">n排的位数最小为3</param>
/// <param name="value1">排在第一位的值,即是1</param>
/// <param name="value2">排在第二位的值,即是1</param>
/// <returns></returns>
static int Menthon2(int n,int value1,int value2)
{
int result = 0;
if (n < 3)
throw new Exception("lenth 位需大3");
if (n != 3)
{
result = Menthon2(n - 1, value2, value2 + value1);
}
else
{
result = value1 + value2;
}
return result;
}
/// <summary>
/// 1-2+3-4+5-6..n求当n为某个值是该表达式的值。
/// </summary>
/// <param name="n">n的具体值</param>
/// <param name="result">初始化的值为0</param>
/// <returns></returns>
static int Menthon3(int n, int result)
{
if (n < 0)
throw new Exception("n需大于0");
if (n > 0)
{
if (n % 2== 0)
{
result = result - n;
}
else {
result= result + n;
}
result= Menthon3(n-1,result);
}
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("测试方法1");
string[] strs = new string[] {"A","B","C","D","E" };
Console.WriteLine("升序排序前的数组:");
Print<string>(strs);
Console.WriteLine("升序排序后的数组:");
string[] result = Menthon1<string>(strs, 0, 1);
Print<string>(result);
Console.WriteLine("再降序排序升序排序后的数组:");
result = Menthon1<string>(result, 0, 0);
Print<string>(result);
Console.WriteLine("测试方法2");
Console.WriteLine("求第6位的值:");
int result1 = Menthon2(6,1,1);
Console.WriteLine(result1);
Console.WriteLine("测试方法3");
Console.WriteLine("当n为5时表达式的值为:");
int result2 = Menthon3(5,0);
Console.WriteLine(result2);
Console.Read();
}
/// <summary>
/// 显示数组的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="values"></param>
static void Print<T>(T[] values)
{
for (int i = 0; i < values.Length;i++ )
{
Console.WriteLine(values[i].ToString());
}
}
/// <summary>
/// 递归的方法实现冒泡法排序
/// </summary>
/// <typeparam name="T">排序数组的类型</typeparam>
/// <param name="values">排序的数组</param>
/// <param name="start">开始的索引号</param>
/// <param name="type">排序的类型,1为升序,0为降序</param>
/// <returns></returns>
static T[] Menthon1<T>(T[] values,int start,int type)
{
T temp;
for (int i = start + 1; i < values.Length;i++ )
{
if (type.Equals(1))
{
if(values[i].ToString().CompareTo(values[start].ToString()).Equals(1))
{
temp= values[start];
values[start] = values[i];
values[i] = temp;
}
}
else
{
if (values[i].ToString().CompareTo(values[start].ToString()).Equals(-1))
{
temp = values[start];
values[start] = values[i];
values[i] = temp;
}
}
}
if(values.Length-1>start)
{
values = Menthon1<T>(values,start+1,type);
}
return values;
}
/// <summary>
/// 1,1,2,3,5,8,13n求N的值
/// </summary>
/// <param name="n">n排的位数最小为3</param>
/// <param name="value1">排在第一位的值,即是1</param>
/// <param name="value2">排在第二位的值,即是1</param>
/// <returns></returns>
static int Menthon2(int n,int value1,int value2)
{
int result = 0;
if (n < 3)
throw new Exception("lenth 位需大3");
if (n != 3)
{
result = Menthon2(n - 1, value2, value2 + value1);
}
else
{
result = value1 + value2;
}
return result;
}
/// <summary>
/// 1-2+3-4+5-6..n求当n为某个值是该表达式的值。
/// </summary>
/// <param name="n">n的具体值</param>
/// <param name="result">初始化的值为0</param>
/// <returns></returns>
static int Menthon3(int n, int result)
{
if (n < 0)
throw new Exception("n需大于0");
if (n > 0)
{
if (n % 2== 0)
{
result = result - n;
}
else {
result= result + n;
}
result= Menthon3(n-1,result);
}
return result;
}
}
}
测试的结果如下:
测试方法1
升序排序前的数组:
A
B
C
D
E
升序排序后的数组:
E
D
C
B
A
再降序排序升序排序后的数组:
A
B
C
D
E
测试方法2
求第6位的值:
8
测试方法3
当n为5时表达式的值为:
3