C#面试常见题目源代码汇总

本文介绍了使用集合实现基数和偶数分离、从数组中筛选基数、数组元素转字符串及字符映射转换、字符串中字符计数、字符映射转换算法、字符计数不区分大小写、集合合并去重等算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;


namespace InterviewTest
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 
            把分拣基数、偶数的联系用集合实现。int []nums ={1,2,3,4,5,6,7,8,9};基数在左边,偶数在右边
            //int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            //List<int> listJI = new List<int>();
            //List<int> listOU = new List<int>();
            //for (int i = 0; i < nums.Length; i++)
            //{
            //    if (nums[i] % 2 == 0)
            //    {
            //        listOU.Add(nums[i]);
            //    }
            //    else
            //    {
            //        listJI.Add(nums[i]);
            //    }
            //}
            //listJI.AddRange(listOU);
            //foreach (var item in listJI)
            //{
            //    Console.Write(item + " ");
            //}
            #endregion


            #region 
            将int数组中的基数放到一个新的int数组中返回。
            将数组中奇数取出来放到放到一个集合中,最终将集合转换成数组


            //int[] nums = { 1, 2, 4, 5, 3, 6, 7, 8, 9 };
            //List<int> listJS = new List<int>();
            //for (int i = 0; i < nums.Length; i++)
            //{
            //    if (nums[i] % 2 != 0)
            //    {
            //        listJS.Add(nums[i]);
            //    }
            //}
            //int[] numsNew = listJS.ToArray();
            //for (int i = 0; i < numsNew.Length; i++)
            //{
            //    Console.Write(numsNew[i] + " ");
            //} 
            #endregion


            #region 
            从一个整数的List<int>中找出最大值,最小值
            //List<int> list = new List<int>() { 531, 2, 4, 5, 6, 7, 8, 3, 12, 53, 22, 63, 12, 63, 13, 74, 234 };
            //int max = int.MinValue;
            //int min = int.MaxValue;
            //for (int i = 0; i < list.Count; i++)
            //{
            //    if (list[i] > max)
            //    {
            //        max = list[i];
            //    }
            //    if (list[i] < min)
            //    {
            //        min = list[i];
            //    }
            //}
            //Console.WriteLine("531, 2, 4, 5, 6, 7, 8, 3, 12, 53, 22, 63, 12, 63, 13, 74, 234中\r\n最大值是:{0},最小值是:{1}", max, min); 
            #endregion


            #region 
            把123转换为壹贰叁 0零 1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖
            //Console.WriteLine("请输入一个整数:");
            //string strinput = Console.ReadLine();
            //string str = "0零 1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖";
            //string[] strNew = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            //string strNews = "";
            //Dictionary<char, char> dic = new Dictionary<char, char>();
            //for (int i = 0; i < strNew.Length; i++)
            //{
            //    dic.Add(strNew[i][0], strNew[i][1]);
            //}
            //for (int i = 0; i < strinput.Length; i++)
            //{
            //    if (dic.Keys.Contains(strinput[i]))
            //    {
            //        Console.Write(dic[strinput[i]]);
            //    }
            //    else
            //    {
            //        Console.Write(strinput[i]);
            //    }
            //} 
            #endregion


            #region 
            计算字符串中每个字符出现的次数,不区分大小写"Welcome to chinese"
            //string str = "Welcome to Chinese";
            //Dictionary<char, int> dic = new Dictionary<char, int>();
            //int count = 0;
            //for (int i = 0; i < str.Length; i++)
            //{
            //    if (str[i] == ' ')
            //    {
            //        continue;
            //    }
            //    if (dic.Keys.Contains(str[i]))
            //    {
            //        count++;
            //    }
            //    else
            //    {
            //        dic[str[i]] = 1;
            //    }
            //}


            //foreach (KeyValuePair<char, int> kv in dic)
            //{
            //    Console.WriteLine("字母{0}出现了{1}次", kv.Key, kv.Value);
            //}
            #endregion


            #region 
            //把两个ArrayList集合{"a","b","c","d","e"}和{"d","e","f","g","h"}把这两个集合去除重复项后合并为一个集合
            ArrayList listOne = new ArrayList() { "a", "b", "c", "d", "e" };
            ArrayList listTwo = new ArrayList() { "d", "e", "f", "g", "h" };
            for (int i = 0; i < listOne.Count; i++)
            {
                if (!listTwo.Contains(listOne[i]))
                {
                    listTwo.Add(listOne[i]);
                }
            }
            for (int i = 0; i < listTwo.Count; i++)
            {
                Console.Write(listTwo[i] + " ");
            }
            #endregion


            Console.ReadKey();
        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值