C# 14道编程题练习

C# 语言不允许 数值类型隐式转换为 char 类型

  1. 声明两个变量:int n1=10,n2=20;要求将两个变量交换,最后输出n1为20,n2为10。扩展(*):不使用第三个变量如何交换?
using System;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {
            int n1 = 10, n2 = 20;
            n1 = n2 - n1;  // n1 = 10
            n2 = n2 - n1;  // n2 = 10
            n1 = n1 + n2;  // n1 = 20
        }
    }
}
  1. 用方法来实现:将上题的交换操作封装成一个方法,方法有两个参数n1,n2,在方法中将n1和n2交换。(提示:使用ref)。
using System;

namespace ConsoleApp1
{

    class Program
    {
        public static void swap(ref int A, ref int B)
        {
            A = B - A;  // 10
            B = B - A;  // 10
            A = A + B;  // 20
        }

        static void Main(string[] args)
        {
            int n1 = 10, n2 = 20;
            swap(ref n1,
                 ref n2);
            Console.WriteLine("n1 = {0}, n2 = {1}", n1, n2);
        }
    }
}
  1. 用方法实现:接收两个int类型的参数,在该方法中计算这两个参数的加、减、乘、除运算的结果,最后将所有结果返回。(提示:out)。
using System;

namespace ConsoleApp1
{

    class NumberManipulator
    {
        public void calculate(int a, int b, out int add, out int sub, out int mul, out int div)
        {
            add = a + b;
            sub = a - b;
            mul = a * b;
            div = a / b;
        }

        static void Main(string[] args)
        {
            NumberManipulator n = new NumberManipulator();
            /* 局部变量定义 */
            int a = 10, b = 20;
            int add, sub, mul, div;  // 加 减 乘 除
            /* 调用函数来获取值 */
            n.calculate(a, b, out add, out sub, out mul, out div);
            Console.WriteLine("参数1= {0}
参数2= {1}
", a, b);
            Console.WriteLine("加法运算结果:{0} ", add);
            Console.WriteLine("减法运算结果:{0} ", sub);
            Console.WriteLine("乘法运算结果:{0} ", mul);
            Console.WriteLine("除法运算结果:{0} ", div);
            
        }
    }
}

  1. 用方法来实现:计算两个数的最大值。扩展(*):计算任意多个数间的最大值(提示:params)。
using System;

namespace ConsoleApp1
{

    class NumberManipulator
    {
        public static void Max(int num1, int num2, int num3, out int max)
        {
            // 多重交叉 比较
            max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : (num2 > num3) ? num2 : num3;
        }
        static void Main(string[] args)
        {
            int num1 = 10;
            int num2 = 20;
            int num3 = 37;
            int max;
            // 使用输出型函数
            Max(num1, num2, num3, out max);
            Console.WriteLine("最大值为: " + max);
            
        }
    }
}
  1. 求斐波那契数列前20项的和。
using System;

namespace ConsoleApp1
{

    class NumberManipulator
    {
        public static void Fibonacci(int n, out int sum)
        {
            sum = 0;
            int x = 0, y = 1;
            for(int j = 1; j < n; j++)
            {
                y += x;   
                x = y - x;
                sum += y;
            }
        }
        static void Main(string[] args)
        {
            int N = 20; // 20 项
            int sum;  // 和
            Fibonacci(N, out sum);
            Console.WriteLine("斐波那契数列前20项和为: " + sum);
        }
    }
}
  1. 有如下字符串:"患者:“大夫,我咳嗽得很重。”     大夫:“你多大年记?”     患者:“七十五岁。”     大夫:“二十岁咳嗽吗”患者:“不咳嗽。”     大夫:“四十岁时咳嗽吗?”     患者:“也不咳嗽。”     大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"。需求:请统计出该字符中“咳嗽”二字的出现次数,以及每次“咳嗽”出现的索引位置。
using System;

namespace ConsoleApp1
{

    class NumberManipulator
    {
        static void Main(String[] args)
        {
            int count = 0;  // 咳嗽出现次数
            int[] index = new int[100]; // 下标数组
            String str = "患者:“大夫,我咳嗽得很重。”     大夫:“你多大年记?”     患者:“七十五岁。”     大夫:“二十岁咳嗽吗”患者:“不咳嗽。”     大夫:“四十岁时咳嗽吗?”     患者:“也不咳嗽。”     大夫:“那现在不咳嗽,还要等到什么时咳嗽?”";
            char[] chr = str.ToCharArray();
            for (int i = 1; i < chr.Length; i++)
            {
                String letter = chr[i - 1].ToString() + chr[i].ToString();
                if (letter == "咳嗽")
                {
                    index[count] = i;
                    count++;
                }
            }
            Console.WriteLine("咳嗽出现的次数: " + count);
            for(int i = 0; i < count; i++)
            {
                Console.WriteLine("下标索引位置: " + index[i]);
            }
        }
    }
}

  1. 从日期字符串"2019年10月1日"中把年月日的数据分别取出来,打印到控制台。
using System;

namespace ConsoleApp1
{

    class NumberManipulator
    {
        static void Main(String[] args)
        {
            // 设置时间
            DateTime dt = DateTime.Parse("2019年10月1日");
            Console.WriteLine("年: " + dt.Year);
            Console.WriteLine("月: " + dt.Month);
            Console.WriteLine("日: " + dt.Day);
        }
    }
}
  1. 分拣奇偶数。将字符串"1 2 3 4 5 6 7 8 9 10"中的数据按照“奇数在前、偶数在后”的格式进行调整。(提示:List
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
    class NumberManipulator
    {
        public static void adjust(String str)
        {
            List<int> result = new List<int>();
            // 提取其中的数字字符串成为字符串数组
            String[] spilt = str.Split(" ");
            for (int i = 0; i < spilt.Length; i++)
            {
                int num = int.Parse(spilt[i]);
                if ( num % 2 == 1)
                {
                    result.Add(num);
                }
            }
            for (int i = 0; i < spilt.Length; i++)
            {
                int num = int.Parse(spilt[i]);
                if (num % 2 == 0)
                {
                    result.Add(num);
                }
            }
            foreach (int item in result)
            {
                Console.Write(item + " ");
            }
        }
        static void Main(String[] args)
        {
            // 奇数在前,偶数在后
            adjust("1 2 3 4 5 6 7 8 9 10");
        }
    }
}

  1. 某int集合中有10个不重复的整数,将其中的奇数挑选出来,保存在一个int数组中。(提示:List
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
    class NumberManipulator
    {
        
        static void Main(String[] args)
        {
            int[] list = new int[]{ 1, 3, 5, 7, 10, 2, 4, 6, 8};
            List<int> L = new List<int>();
            for (int i = 0; i < list.Length; i++)
            {
                if (list[i] % 2 == 1)
                    L.Add(list[i]);
            }
            Console.WriteLine("所有的奇数:");
            foreach (int item in L)
            {
                Console.Write(item + " ");
            }
        }
    }
}

  1. 接收用户输入的“123”,输出“壹贰叁”。(提示:Dictionary<K,V>  "0零 1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖")
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
    class NumberManipulator
    {
        public static void Transform(String input, Dictionary<int, String> dic)
        {
            foreach (char item in input)
            {
                Console.Write(dic[int.Parse(item.ToString())]);
            }
        }
        static void Main(String[] args)
        {
            Dictionary<int, String> dic = new Dictionary<int, string>();
            // 先将匹配字典输入
            dic.Add(0, "零");
            dic.Add(1, "壹");
            dic.Add(2, "贰");
            dic.Add(3, "叁");
            dic.Add(4, "肆");
            dic.Add(5, "伍");
            dic.Add(6, "陆");
            dic.Add(7, "柒");
            dic.Add(8, "捌");
            dic.Add(9, "玖");
            Console.Write("请输入数字: ");
            String input = System.Console.ReadLine();
            Transform(input, dic);
           
        }
    }
}

  1. 计算字符串中每种字母出现的次数。"Hello! Welcome to China!  How are you ?  Fine!  And you?"(扩展:不区分大小写)(提示:Dictionary<K,V>)
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
    class NumberManipulator
    {
        public static void All(String str)
        {
            // 创建泛型哈希表,Key类型为char,Value类型为int
            Dictionary<char, int> dic = new Dictionary<char, int>();
            // 为了避免不必要的麻烦,所有都小写
            str.ToLower();
            foreach (char item in str)
            {
                // 判断是否为字母
                if (char.IsLetter(item))
                {
                    // 判断是否存在
                    if (!dic.ContainsKey(item))
                    {
                        dic.Add(item, 1);
                    }
                    // 存在 则 + 1
                    else
                    {
                        dic[item] += 1;
                    }
                }
            }
            // 通过KeyValuePair遍历元素
            foreach (KeyValuePair<char, int>dict in dic)
            {
                Console.WriteLine("字母 {0} 出现的个数: {1}", dict.Key, dict.Value);
            }
        }

        static void Main(String[] args)
        {
            String str = "Hello! Welcome to China!  How are you ?  Fine!  And you?";

            All(str);

        }
    }
}

  1. 设计方法实现,将中文日期转换为数字格式的日期。比如:“二零一八年九月二十日”应转换为“2018-9-20”。("零0 一1 二2 三3 四4 五5 六6 七7 八8 九9", 难点:“十”的转换)
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
    class NumberManipulator
    {
        public static void transform(String str)
        {
        Dictionary<char, int> dic = new Dictionary<char, int>();
            char[] temp1 = new char[]{
                '零',
                '一',
                '二',
                '三',
                '四',
                '五',
                '六',
                '七',
                '八',
                '九',
                '十',
            };
            for (int i = 0; i < 11; i++)
            {
                dic.Add(temp1[i], i);
            }
            dic.Add('年', 0);
            dic.Add('月', 0);
            dic.Add('日', 0);
            String result = "";
            char[] chr = str.ToCharArray();
            for (int i = 1; i < chr.Length; i++)
            {
              
                if (chr[i - 1] == '年' || chr[i - 1] == '月')
                {
                    result += "-";
                }
                else if (chr[i - 1] == '日')
                {
                    result += "";
                }
                else if(dic[chr[i - 1]] == 10)
                {
                    int temps = dic[chr[i - 2]] * 10;
                    if (chr[i] != '日')
                    {
                        temps += dic[chr[i]];
                        i++;
                    }
                    result += temps;

                }
                else if(dic[chr[i]] != 10)
                {
                    result += dic[chr[i - 1]];
                }
                
            }
            Console.WriteLine(result);
        }

        static void Main(String[] args)
        {
            string str = "二零一八年九月二十八日";
            transform(str);
        }
    }
}

难顶,先这样吧

原文地址:https://www.cnblogs.com/xmdykf/p/12327705.html