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}\n參數2= {1}\n", 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);
        }
    }
}

難頂,先這樣吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM