VR_C#拒绝摸鱼第五天

154 阅读1分钟

冒泡排序

            int[] num = { 2, 13, 22, 56, 6, 9, 3, 11 };//自定义输入char[] num = Console.ReadLine().ToCharArray()
            for (int i = 0; i < num.Length - 1; i++)//多少轮
            {
                for (int j = 0; j < num.Length - 1 - i; j++) //一轮多少次
                {
                    if (num[j] > num[j + 1])//换值
                    {
                        int temp = num[j + 1];
                        num[j + 1] = num[j];
                        num[j] = temp;
                    }
                }
            }
            Console.WriteLine(string.Join("-", num));//转成字符-连接输出
            

Array.Sort()示例

            Array.Sort(num);//数组排序,会改变原来数组
            Console.WriteLine(string.Join("-", num));

有序数组中插入一个数

            int[] num = { 1, 2, 3, 4, 5, 6 };//有序数组中插入一个数,依旧有序
            int x = Convert.ToInt32(Console.ReadLine());
            int n = 0;//记录x要插入的位置
            for(int i = 0; i < num.Length-1; i++)//num.Length-1,减1怕num[i+1]溢出
            {
                if (num[i] <= x && x < num[i + 1])
                {
                    n = i ;
                }
            }
            if (x >= num[num.Length - 1])
            {
                n=num.Length;
            }
            int[] num1 = new int[num.Length + 1];
            for (int i = 0; i <= n; i++)
            {
                num1[i] = num[i];
            }
            num1[n+1] = x;
            for (int i = n+2; i < num1.Length; i++)
            {
                num1[i] = num[i-1];
            }
            Console.WriteLine(string.Join("-", num1));

无序数组中插入一个数

            Console.WriteLine("请输入一组用空格隔开的数据:");
            string str = Console.ReadLine();//获取控制台输入一组数据
            string[] strArray = str.Split(" ");//将数字用空格分割成数组的元素
            int[] numArray = new int[strArray.Length];//创建一个整型数组
            for (int i = 0; i < strArray.Length; i++)//遍历,给numArray赋值
            {
                numArray[i] = Convert.ToInt32(strArray[i]);
            }
            Array.Sort(numArray);//给数组排序
            Console.WriteLine("请输入要插入的数据:");
            int num = Convert.ToInt32(Console.ReadLine());//输入要插入的数
            int numIndex = 0;//定义一个变量记录插入的位置
            for(int i = 0; i < numArray.Length - 1; i++)//循环比较元素,寻找插入位置
            {
                if (numArray[i] <= num && num <= numArray[i + 1])
                {
                    numIndex = i + 1;
                    break;
                }
            }
            //如果没有执行上面的if语句则有两种情况,在开头或者在最后,在开头则为0(初始值为0不用判断)
            if (num > numArray[numArray.Length - 1])
            {
                numIndex = numArray.Length;
            }
            int[] num2Array = new int[numArray.Length + 1];//创建新数组
            for(int i = 0; i <= numIndex - 1; i++)//将前numIndex项赋值给num2Array
            {
                num2Array[i] = numArray[i];
            }
            num2Array[numIndex] = num;//插入赋值
            for(int i = numIndex+1; i < num2Array.Length; i++)//将numIndex项后的数据赋值给num2Array
            {
                num2Array[i] = numArray[i - 1];//num2Array插入了以为,所以比numArray多一
            }
            Console.WriteLine(string.Join("-", num2Array));

判断是否是合法变量名

合法的变量名:首个字符不能是数字,有数字,字母下划线组成,输入一个字符串,判断是否是合法变量名

            Console.WriteLine("判断是否为合法标识符,请输入一个字符串:");
            string str = Console.ReadLine();
            Boolean isLeagal = false;//创建一个中间变量来记录是否是合法标识
            for(int i = 0; i < str.Length; i++)
            {
                if(48<=str[i]&&str[i]<=57||65<=str[i]&& str[i] <= 90|| 97 <= str[i] && str[i] <= 122 || str[i] == 95)
                {
                    if(48 <= str[0] && str[0] <= 57)
                    {
                        break;
                    }
                    isLeagal = true;
                }
                else
                {
                    isLeagal = false;
                    break;//只要有一个合法就跳出循环
                }
            }
            if (isLeagal)
            {
                Console.WriteLine("这是一个合法的标识符");
            }
            else
                Console.WriteLine("这是一个不合法的标识符");

判断是否是回文串,前后字符对称就是回文

            Console.WriteLine("请输入一串字符,判断是否是回文字符:");
            string str = Console.ReadLine();
            Boolean isHW = true;
            for(int i=0; i < str.Length / 2; i++)
            {
                if (str[i] != str[str.Length - 1 - i])
                {
                    isHW = false;//不同说明不是回文,直接跳出循环
                    break;
                }
            }
            if (isHW==true)
            {
                Console.WriteLine("该字符串是回文字符");
            }
            else
                Console.WriteLine("该字符串不是回文字符");

计算一组数据的连续下降次数

            Console.WriteLine("请输入一组用空格分割的数据:");
            string str = Console.ReadLine();
            string[] strArray = str.Split(" ");
            int[] numArray = new int[strArray.Length];
            for(int i = 0; i < numArray.Length; i++)
            {
                numArray[i] = Convert.ToInt32(strArray[i]);
            }
            int days=1, max_days=0;
            for(int i = 0; i < numArray.Length-1; i++)
            {
                if (numArray[i] > numArray[i + 1])
                {
                    days++;
                    if (max_days < days)
                        max_days = days;
                }
                else
                    days = 1;
            }
            Console.WriteLine("最多连续下降个数为{0}",max_days);

小明存钱

image.png

            onsole.WriteLine("请输入小明12个月以内的预算:");
            string str = Console.ReadLine();
            string[] strArray = str.Split(" ");
            int[] budget = new int[strArray.Length];
            for(int i = 0; i < budget.Length; i++)
            {
                budget[i] = Convert.ToInt32(strArray[i]);
            }
            int s_money = 0,c_money=0;//剩余的钱,存款
            for(int i = 0; i < budget.Length; i++)
            {
                s_money = 300 + s_money - budget[i];
                if (s_money >= 100) //满足存款要求
                {
                    int temp = 100 * (s_money / 100);
                    s_money -= temp;//除去存款剩余的钱
                    c_money += temp;//存款数
                }
                else if (s_money < 0)
                {
                    Console.WriteLine("{0}月超出预算",i + 1);
                }
            }
            Console.WriteLine("存款为{0},小明还剩{1}块钱",c_money,s_money);
            if (c_money > 0)
                Console.WriteLine("年末妈妈将给小明{0}块钱", c_money * (1.2));

求最大篮球的单词数(改进中)

image.png

            Console.WriteLine("请输入一组字符串:");
            string str = Console.ReadLine();
            //string[] strArray = str.Split();
            //每个字母出现的次数放到数组中
            int[] num = new int[7];
            //遍历字符串 把每个字母出现的次数放到对应的下标里
            for(int i = 0; i < str.Length; i++)
            {
                switch (str[i])
                {
                    case 'b':
                        num[0]++;
                        continue;
                    case 'a':
                        num[1]++;
                        continue;
                    case 's':
                        num[2]++;
                        continue;
                    case 'k':
                        num[3]++;
                        continue;
                    case 'e':
                        num[4]++;
                        continue;
                    case 't':
                        num[5]++;
                        continue;
                    case 'l':
                        num[6]++;
                        continue;
                    default:
                        continue;
                }
            }
            num[0] /= 2;
            num[1] /= 2;
            num[6] /= 2;
            int minNum = num.Min();//Min()用不了,则在开头加上using System.Linq;
            Console.WriteLine("可组成{0}个单词",minNum);