using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//冒泡排序
Console.WriteLine("冒泡排序");
int[] numbers = { 1, 24, 23, 98, 89, 77, 99, 100 };
BubbleSort(numbers);
foreach (int num in numbers)
{
Console.Write(num + " | ");
}
Console.WriteLine("\n请按任意键继续...");
//递归算法
Console.WriteLine("递归算法");
Console.WriteLine(Recursions(9));
//大小写互转
Console.WriteLine("递归算法");
string str = "aaaBBB";
Console.WriteLine(ToggleCase(str));
Console.Read();
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="numbers"></param>
public static void BubbleSort(int[] numbers)
{
int count = numbers.Length;
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count - 1; j++)
{
if (numbers[j] < numbers[j + 1])
{
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
}
/// <summary>
/// 递归算法
/// </summary>
/// <param name="n"></param>
public static int Recursions(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return Recursions(n - 1) + Recursions(n - 2);
}
}
/// <summary>
/// 大小写互转
/// </summary>
/// <param name="str"></param>
public static string ToggleCase(string str)
{
var chars = str.ToCharArray();
for (int i = 0; i < str.Length; i++)
{
var cha = chars[i];
if (cha > 64 && cha < 91)
{
cha = (char)((int)cha + 32);
}
else
{
cha = (char)((int)cha - 32);
}
chars[i] = cha;
}
str = new string(chars);
return str;
}
}
}