C#基础练习

时间:2021-03-29 16:37:39

1、冒泡排序

namespace _0
{
class Program
{
public static int[] BubbleSort(int[] arr)
{
for (int i = ; i < arr.Length - ; i++)
{
for (int j = ; j < arr.Length - ; j++)
{
if (arr[j] > arr[j + ])
{
int temp = arr[j];
arr[j] = arr[j + ];
arr[j + ] = temp;
}
}
}
return arr;
}
static void Main(string[] args)
{
int[] numbers = { , , -, , , , , -, , };
int[] result = BubbleSort(numbers);
for (int i = ; i < result.Length; i++)
{
Console.Write("{0} ", result[i]);
}
Console.ReadKey();
}
}
}

2、100~999之间的水仙花数

namespace _0
{
class Program
{
public static bool Daffodils(int arr)
{
int hundreds = arr / ;
// 获取百位数
int ten = arr / % ;
// 获取十位数
int single = arr % ;
// 获取个位数
int n = (hundreds * hundreds * hundreds) +
(ten * ten * ten) +
(single * single * single);
if (arr == n)
{
return true;
}
return false;
}
static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
bool flag = Daffodils(i);
if (flag)
{
Console.WriteLine("Daffodils: {0}", i);
}
}
Console.ReadKey();
}
}
}

3、取出数组中最大值和最小值

namespace _0
{
class Program
{
public static int[] ReturnMaxAndMin(int[] arr)
{
int[] result = { arr[], arr[] };
for (int i = ; i < arr.Length; i++)
{
if (arr[i] > result[])
{
result[] = arr[i];
}
else if (arr[i] < result[])
{
result[] = arr[i];
}
}
return result;
}
static void Main(string[] args)
{
int[] arrary = { , , , , , , -, , - };
int[] numbers = ReturnMaxAndMin(arrary);
Console.WriteLine("Max number: {0}, Min number: {1}", numbers[], numbers[]);
Console.ReadKey();
}
}
}