C # basic exercises,
1. Bubble Sorting
namespace _0{ class Program { public static int[] BubbleSort(int[] arr) { for (int i = 0; i < arr.Length - 1; i++) { for (int j = 0; j < arr.Length - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; } static void Main(string[] args) { int[] numbers = { 2, 5, -7, 9, 3, 2, 1, -3, 0, 2 }; int[] result = BubbleSort(numbers); for (int i = 0; i < result.Length; i++) { Console.Write("{0} ", result[i]); } Console.ReadKey(); } }}
2. 100 ~ The number of daffodils between 999
Namespace _ 0 {class Program {public static bool Daffodils (int arr) {int hundreds = arr/100;// Obtain hundreds of BITsInt ten = arr/10% 10;// Obtain ten digitsInt single = arr % 10;// Obtain the single digitInt n = (hundreds * hundreds) + (ten * ten) + (single * single); if (arr = n) {return true ;} return false;} static void Main (string [] args) {for (int I = 100; I <999; I ++) {bool flag = Daffodils (I ); if (flag) {Console. writeLine ("Daffodils: {0}", I) ;}} Console. readKey ();}}}
3. Retrieve the maximum and minimum values in the array.
namespace _0{ class Program { public static int[] ReturnMaxAndMin(int[] arr) { int[] result = { arr[0], arr[0] }; for (int i = 0; i < arr.Length; i++) { if (arr[i] > result[0]) { result[0] = arr[i]; } else if (arr[i] < result[1]) { result[1] = arr[i]; } } return result; } static void Main(string[] args) { int[] arrary = { 1, 2, 4, 6, 7, 8, -1, 2, -4 }; int[] numbers = ReturnMaxAndMin(arrary); Console.WriteLine("Max number: {0}, Min number: {1}", numbers[0], numbers[1]); Console.ReadKey(); } }}