C# 练习题

1.最近张三开始研究天⽓的变化。历经千⾟万苦,他收集了连续N(1<N<1000000)天的最⾼⽓温数据。现在他想知道⽓温⼀直上升的最⻓连续天数。
样例输⼊:1 3 5 2 3 5 7 8 6 9
样例输出:5

            string sum = "1 3 7 2 3 5 7 8 6 9";
            int[] temperatures = Array.ConvertAll(sum.Split(' '), int.Parse);
            int longestStreak = 1; // 当前最长连续上升天数,默认为1   2 3 4 5
            int currentStreak = 1; // 当前连续上升天数,默认为1   2 3 1 2 3 4 5
            for (int i = 1; i < temperatures.Length; i++)
            {
                if (temperatures[i] > temperatures[i - 1])
                {
                    currentStreak++;// 如果温度上升,当前连续上升天数加1
                }
                else
                {
                    currentStreak = 1;
                }
                // 更新最长连续上升天数
                if (currentStreak > longestStreak) longestStreak = currentStreak;


            }
            Console.WriteLine(longestStreak); // 输出最长连续上升天数

2.描述给定⼀个只包含⼩写字⺟的字符串,请你找出第⼀个仅出现⼀次的字符。如果没有,输no。

            string input = "abaccabad";
            char result = FindFirstUniqueCharacter(input);

            if (result == '\0')
            {
                Console.WriteLine("no");
            }
            else
            {
                Console.WriteLine(result);
            }

        public static char FindFirstUniqueCharacter(string str)
        {
            int[] charCount = new int[26];

            foreach (char c in str)
            {
                int index = c - 'a';
                charCount[index]++;
            }
            foreach (char c in str)
            {
                int index = c - 'a';
                if (charCount[index] == 1)
                {
                    return c;
                }
            }
            return '\0';
        }

3.有n(n<=100)个整数,已经按照从⼩到⼤顺序排列好,现在另外给⼀个整数x,请将该数插⼊到
序列中,并使新的序列仍然有序。
输出新的序列


            Console.WriteLine("请输入数组的长度"); 
            int n = int.Parse(Console.ReadLine());
            int[] sequence = new int[n]; 
            for (int i = 0; i < n; i++)  sequence[i] = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入插入数组里的一个数字");
            int x = int.Parse(Console.ReadLine());
            int[] newSequence = new int[n + 1];
            int insertIndex = 0;
            bool xInserted = false;
            for (int i = 0; i < n; i++)
            {
            if (!xInserted && sequence[i] > x)
             {
                    newSequence[insertIndex++] = x;
                    xInserted = true;
             }
                newSequence[insertIndex++] = sequence[i]; 
            }

            if (!xInserted) newSequence[insertIndex] = x;
            for (int i = 0; i < n + 1; i++) Console.WriteLine(newSequence[i]);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值