C # basic exercises,

Source: Internet
Author: User

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();        }    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.