C#学习第五天

211 阅读2分钟

一、冒泡排序

说明:有一组杂乱的数据,要求从小到大进行排序,这时候我们可以从下标为0的元素开始,相邻两两进行比对,如果前一个元素比后一个元素大则将前一个元素和后一个元素交换值,经过第一轮比对后,我们可以知道最后一个元素一定是数组中最大的值,接下来再比对前n-1个元素,以此类推每次都会得到一个最大值,最后数组就从小到大进行排序了。

 //冒泡排序
            //总比对的轮数等于长度减一    比对的最大下标等于长度-1-已经确定的数-1
            /* string str = Console.ReadLine();
             string[] strArray = str.Split(" ");
             int[] intArray = new int[strArray.Length];
             for (int i = 0; i < intArray.Length; i++)
             {
                 intArray[i] = Convert.ToInt32(strArray[i]);
             }


             //循环数组  判断相邻两个数之间的大小
             for (int i = 1; i<=intArray.Length-1; i++)
             {
                 //比对两个相邻的数
                 for(int j=0; j<intArray.Length-i; j++)  //
                 {
                     if(intArray[j] > intArray[j + 1])   //如果前一个比后一个大那就交换值
                     {
                         int temp = intArray[j];
                         intArray[j] = intArray[j + 1];
                         intArray[j + 1] = temp;
                     }
                 }
             }
             Console.WriteLine(String.Join(" ", intArray));
 */

C#

使用Array.Sort(数组名)对数组进行排序


int[] num = {2, 5, 3, 7, 4, 17, 1};
                        Array.Sort(num);  //会改变原来数组
                        Console.WriteLine(num);

c#

一、练习题

1.练习一

image.png

            /* string str = Console.ReadLine();
             string[] strArray = str.Split(" ");
             int[] intArray = new int[strArray.Length];
             for (int i = 0; i < intArray.Length; i++)
             {
                 intArray[i] = Convert.ToInt32(strArray[i]);
             }

             Array.Sort(intArray);  //排好序的数组   1 34 3 46 67    12

             //先确定好要插入的下标位置
             //先输入一个数
             int m = Convert.ToInt32(Console.ReadLine());
             //定义一个变量存下标位置
             int numIndex = 0;
             //定义一个新数组,长度加一
             int[] numArray = new int[intArray.Length + 1];
             //先确定好要插入的下标位置
             for (int i = 0; i<intArray.Length-1; i++)
             {
                 if (intArray[i]<=m && m <= intArray[i + 1]) 
                 {
                     numIndex = i + 1;
                     break;
                 }
             }
             //循环结束 有两种情况出现就是插入到第一个下标位置前,插入到最后一个下标
             if (intArray[intArray.Length - 1] < m)
             {
                 numIndex = intArray.Length;
             }
             //给数组赋值
             for (int i = 0; i<numIndex; i++)
             {
                 numArray[i] = intArray[i];
             }
             numArray[numIndex] = m;   
             for (int i =numIndex+1; i<numArray.Length; i++)
             {
                 numArray[i] = intArray[i - 1];
             }
             Console.WriteLine(String.Join(" ", numArray));
 */



2.练习二

image.png

3.练习三

image.png

4.练习四

image.png

/* //先创建一个字符串存输入进来的数
            string str = Console.ReadLine();
            string[] strArray = str.Split(" ");
            int[] temp = new int[strArray.Length];
            for(int i = 0; i<strArray.Length; i++)
            {
                temp[i] = Convert.ToInt32(strArray[i]);

            }
            //创建一个变量记录每次降温的天数,降一次温加1
            int count = 1;
            //创建一个变量记录最大连续降温的天数的值
            int maxCount = 1;
            //循环遍历气温数组

            for (int i = 0; i < temp.Length - 1; i++)
            {
                if (temp[i + 1] < temp[i])
                {
                    count++;

                }
                else
                {
                    //需要记录最大值
                    if (maxCount < count)
                    {
                        maxCount = count;
                    }
                    count = 1;

                }


            }
            maxCount = maxCount < count ? count : maxCount;
            Console.WriteLine("最大的连续天数是{0}", maxCount);*/

5.练习五

给定一串只包含小写字母的字符串,计算每个字母在字符串中出现的次数,然后输出只出现2次的字母的个数

6.练习六

image.png image.png

七.练习七

求最大篮球的单词数

图片.png

Console.WriteLine("请输入字符串text:");
            string text = Console.ReadLine();
            int[] count;
            count = new int[7];
            foreach (char c in text)
            {
                if(c == 'a')
                {
                    count[0]++;
                }else if(c == 'b')
                {
                    count[1]++;
                }else if(c == 'e')
                {
                    count[2]++;
                }else if (c == 'k')
                {
                    count[3]++;
                }
                else if (c == 'l')
                {
                    count[4]++;
                }
                else if (c == 's')
                {
                    count[5]++;
                }
                else if (c == 't')
                {
                    count[6]++;
                }
            }
            count[0] /= 2;
            count[1] /= 2;
            count[4] /= 2;
            
            Console.WriteLine("输出:{0}",count.Min());