練習 1:計算圓的周長和面積
改編題目:編寫一個 C# 程序,讓用戶輸入圓的半徑,然后計算并輸出該圓的周長和面積,結果保留兩位小數。
using System;class CircleCalculation
{static void Main(){const double pi = 3.14;Console.Write("請輸入圓的半徑:");int n = int.Parse(Console.ReadLine());double circumference = 2 * pi * n;double area = pi * n * n;Console.WriteLine($"半徑為 {n} 的圓的周長為:{circumference:F2}");Console.WriteLine($"半徑為 {n} 的圓的面積為:{area:F2}");}
}
練習 2:兩個變量值交換(兩種方法)
改編題目:編寫一個 C# 程序,實現兩個整數變量值的交換,分別使用臨時變量和不使用臨時變量兩種方法,并輸出交換后的結果
using System;class VariableSwap
{static void Main(){// 方法一:使用臨時變量int a = 8, b = 19;int temp = a;a = b;b = temp;Console.WriteLine($"使用臨時變量交換后,a={a}, b={b}");// 方法二:不使用臨時變量a = 8;b = 19;a = a + b;b = a - b;a = a - b;Console.WriteLine($"不使用臨時變量交換后,a={a}, b={b}");}
}
練習 3:輸入 3 個數,從小到大輸出
改編題目:編寫一個 C# 程序,讓用戶輸入 3 個整數,然后將這 3 個數從小到大排序并輸出。
using System;class SortThreeNumbers
{static void Main(){Console.Write("請您輸入 3 個整數,用空格區分開:");string[] input = Console.ReadLine().Split(' ');int a = int.Parse(input[0]);int b = int.Parse(input[1]);int c = int.Parse(input[2]);if (a > b){int temp = a;a = b;b = temp;}if (a > c){int temp = a;a = c;c = temp;}if (b > c){int temp = b;b = c;c = temp;}Console.WriteLine($"從小到大排序為:{a} {b} {c}");}
}
練習 4:根據學生成績輸出等級
改編題目:編寫一個 C# 程序,讓用戶輸入一個 0 - 100 之間的學生成績,根據成績輸出對應的等級(60 分以下為不及格,60 - 69 為合格,70 - 79 為良,80 - 89 為優,90 以上為超神)。
using System;class GradeEvaluation
{static void Main(){Console.Write("請輸入學生成績(0 - 100):");int score = int.Parse(Console.ReadLine());if (score < 60){Console.WriteLine("不及格");}else if (score >= 60 && score <= 69){Console.WriteLine("合格");}else if (score >= 70 && score <= 79){Console.WriteLine("良");}else if (score >= 80 && score <= 89){Console.WriteLine("優");}else{Console.WriteLine("超神");}}
}
練習 5:求 0 - 100 之間所有偶數和奇數的和
改編題目:編寫一個 C# 程序,計算 0 - 100 之間所有偶數的和以及所有奇數的和,并分別輸出結果。
using System;class EvenOddSum
{static void Main(){int evenSum = 0;int oddSum = 0;for (int i = 0; i <= 100; i++){if (i % 2 == 0){evenSum += i;}else{oddSum += i;}}Console.WriteLine("0 - 100 之間所有偶數的和為:" + evenSum);Console.WriteLine("0 - 100 之間所有奇數的和為:" + oddSum);}
}
練習 6:求 10 的階乘
改編題目:編寫一個 C# 程序,計算 10 的階乘并輸出結果。
using System;class FactorialCalculation
{static void Main(){int result = 1;for (int i = 1; i <= 10; i++){result *= i;}Console.WriteLine("10 的階乘為:" + result);}
}
練習 7:反向輸出數字
改編題目:編寫一個 C# 程序,讓用戶輸入一個整數,然后將該整數反向輸出。
using System;class ReverseNumber
{static void Main(){Console.Write("請輸入一個整數:");int num = int.Parse(Console.ReadLine());int reversed = 0;while (num != 0){reversed = reversed * 10 + num % 10;num /= 10;}Console.WriteLine("反向輸出的數字為:" + reversed);}
}
練習 8:輸出指定形狀
改編題目:編寫一個 C# 程序,在屏幕上輸出如下形狀:
*
**
***
****
*****
******
using System;class ShapeOutput1
{static void Main(){for (int i = 1; i <= 6; i++){for (int j = 0; j < i; j++){Console.Write("*");}Console.WriteLine();}}
}
練習 10:單循環輸出指定形狀
改編題目:編寫一個 C# 程序,使用單循環在屏幕上輸出如下形狀:
*
**
***
****
*****
******
using System;class ShapeOutputSingleLoop
{static void Main(){string stars = "";for (int i = 1; i <= 6; i++){stars += "*";Console.WriteLine(stars);}}
}
練習 11:輸出指定對稱形狀
改編題目:編寫一個 C# 程序,在屏幕上輸出如下形狀:
*
***
*****
*******
*********
*******
*****
***
*
using System;class SymmetricShapeOutput
{static void Main(){int maxStars = 9;for (int i = 1; i <= maxStars; i += 2){for (int j = 0; j < (maxStars - i) / 2; j++){Console.Write(" ");}for (int k = 0; k < i; k++){Console.Write("*");}Console.WriteLine();}for (int i = maxStars - 2; i >= 1; i -= 2){for (int j = 0; j < (maxStars - i) / 2; j++){Console.Write(" ");}for (int k = 0; k < i; k++){Console.Write("*");}Console.WriteLine();}}
}
練習 12:打印 9 * 9 乘法表
改編題目:編寫一個 C# 程序,打印 9 * 9 乘法表。
using System;class MultiplicationTable
{static void Main(){for (int i = 1; i <= 9; i++){for (int j = 1; j <= i; j++){Console.Write($"{j} * {i} = {i * j}\t");}Console.WriteLine();}}
}
練習 13:找出 100 到 999 之間的水仙花數(兩種方法)
改編題目:編寫一個 C# 程序,找出 100 到 999 之間的所有水仙花數,分別使用單循環和嵌套循環兩種方法。
using System;class NarcissisticNumbersSingleLoop
{static void Main(){for (int num = 100; num < 1000; num++){int hundreds = num / 100;int tens = (num / 10) % 10;int units = num % 10;if (hundreds * hundreds * hundreds + tens * tens * tens + units * units * units == num){Console.WriteLine(num);}}}
}
C# 代碼(嵌套循環):
using System;class NarcissisticNumbersNestedLoop
{static void Main(){for (int i = 1; i <= 9; i++){for (int j = 0; j <= 9; j++){for (int k = 0; k <= 9; k++){int num = i * 100 + j * 10 + k;if (i * i * i + j * j * j + k * k * k == num){Console.WriteLine(num);}}}}}
}
練習 14:組成不重復的三位數
改編題目:編寫一個 C# 程序,使用 1、2、3、4 四個數字,找出能組成多少個不相同且無重復數字的三位數,并輸出這些三位數。
using System;class ThreeDigitNumbers
{static void Main(){int count = 0;for (int i = 1; i <= 4; i++){for (int j = 1; j <= 4; j++){if (j == i) continue;for (int k = 1; k <= 4; k++){if (k == i || k == j) continue;int num = i * 100 + j * 10 + k;Console.WriteLine(num);count++;}}}Console.WriteLine("能組成的不相同且無重復數字的三位數共有:" + count + " 個");}
}
練習 15:歌星大賽打分計算平均分
改編題目:編寫一個 C# 程序,模擬歌星大賽打分過程。假設有 100 個評委給選手打分,分值在 1 - 100 之間,去掉一個最高分和一個最低分后,計算剩余 98 個分數的平均分作為選手的最后得分。
using System;class SingerCompetitionScore
{static void Main(){Random random = new Random();int[] scores = new int[100];for (int i = 0; i < 100; i++){scores[i] = random.Next(1, 101);}int maxScore = scores[0];int minScore = scores[0];int totalScore = 0;foreach (int score in scores){if (score > maxScore) maxScore = score;if (score < minScore) minScore = score;totalScore += score;}totalScore = totalScore - maxScore - minScore;double averageScore = (double)totalScore / 98;Console.WriteLine("選手的最后得分是:" + averageScore);}
}
練習 16:可樂兌換問題
改編題目:已知 3 個可樂瓶可以換一瓶可樂,現在有 364 瓶可樂,編寫一個 C# 程序,計算一共可以喝多少瓶可樂,最后剩下幾個空瓶。
using System;class ColaExchange
{static void Main(){int totalCola = 364;int emptyBottles = 364;while (emptyBottles >= 3){int exchangedCola = emptyBottles / 3;totalCola += exchangedCola;emptyBottles = emptyBottles % 3 + exchangedCola;}Console.WriteLine("一共可以喝 " + totalCola + " 瓶可樂,剩下 " + emptyBottles + " 個空瓶。");}
}
練習 17:兔子繁殖問題
改編題目:有一對兔子,從出生后第 3 個月起,每個月都生一對兔子;小兔子長到第三個月后,每個月又生一對兔子。假如兔子都不死,編寫一個 C# 程序,計算第 1 到第 20 個月里,每個月的兔子總數。
using System;class RabbitReproduction
{static void Main(){int[] rabbits = new int[20];rabbits[0] = 1;rabbits[1] = 1;for (int i = 2; i < 20; i++){rabbits[i] = rabbits[i - 1] + rabbits[i - 2];}for (int i = 0; i < 20; i++){Console.WriteLine($"第 {i + 1} 個月的兔子總數為:{rabbits[i]} 對");}}
}
練習 18:百錢百雞問題
改編題目:編寫一個 C# 程序,解決百錢百雞問題。已知公雞 5 元一只,母雞 3 元一只,小雞 1 元 3 只,現在有 100 元錢要買 100 只雞,找出所有可能的購買方案并輸出。
C# 代碼:
using System;class HundredMoneyHundredChickens
{static void Main(){for (int rooster = 0; rooster <= 20; rooster++){for (int hen = 0; hen <= 33; hen++){int chick = 100 - rooster - hen;if (chick % 3 == 0 && rooster * 5 + hen * 3 + chick / 3 == 100){Console.WriteLine($"公雞:{rooster} 只,母雞:{hen} 只,小雞:{chick} 只");}}}}
}
練習 19:輸出一個 120 - 245 之間的隨機數
改編題目:編寫一個 C# 程序,模擬抽獎活動,每次運行程序時,從 120 到 245 這個區間內隨機抽取一個幸運號碼并輸出。
using System;class RandomNumberInRange
{static void Main(){Random random = new Random();int luckyNumber = random.Next(120, 246);Console.WriteLine($"本次抽獎的幸運號碼是: {luckyNumber}");}
}
練習 20:猜數字游戲
設計一個 C# 程序實現猜數字游戲。程序會隨機生成一個 0 到 50 之間的整數,玩家需要猜測這個數字。每次猜測后,程序會提示玩家猜大了、猜小了還是猜對了,直到玩家猜對為止。同時,記錄玩家猜測的次數并在猜對后輸出。
using System;class GuessTheNumberGame
{static void Main(){Random random = new Random();int secretNumber = random.Next(0, 51);int guess;int attempts = 0;Console.WriteLine("我心里想了一個 0 到 50 之間的數字,你可以開始猜啦!");do{Console.Write("請輸入你猜測的數字: ");guess = int.Parse(Console.ReadLine());attempts++;if (guess < secretNumber){Console.WriteLine($"你猜小了,這個數字比 {guess} 大。");}else if (guess > secretNumber){Console.WriteLine($"你猜大了,這個數字比 {guess} 小。");}else{Console.WriteLine($"恭喜你,猜對了!這個數字就是 {secretNumber}。你一共用了 {attempts} 次猜測。");}} while (guess != secretNumber);}
}
練習 21:初始化數組并倒序輸出
改編題目:編寫一個 C# 程序,創建一個包含 10 個元素的整數數組,使用隨機數為數組元素賦值(范圍為 1 - 100),然后將數組元素倒序排列并輸出。
using System;class ReverseRandomArray
{static void Main(){int[] array = new int[10];Random random = new Random();// 用隨機數初始化數組for (int i = 0; i < array.Length; i++){array[i] = random.Next(1, 101);}// 倒序數組Array.Reverse(array);// 輸出倒序后的數組Console.WriteLine("倒序后的數組元素為:");foreach (int num in array){Console.Write(num + " ");}Console.WriteLine();}
}
練習 22:初始化數組并亂序輸出(兩種方法)
改編題目:在 C# 中,創建一個包含 10 個元素的數組,元素值為 0 到 10,然后使用兩種不同的方法將數組元素打亂順序并輸出。
using System;class ShuffleArray
{static void Main(){int[] array = new int[10];for (int i = 0; i < array.Length; i++){array[i] = i;}// 方法一:使用 Random 類交換元素int[] shuffledArray1 = (int[])array.Clone();Random random = new Random();for (int i = shuffledArray1.Length - 1; i > 0; i--){int j = random.Next(i + 1);int temp = shuffledArray1[i];shuffledArray1[i] = shuffledArray1[j];shuffledArray1[j] = temp;}Console.WriteLine("方法一亂序后的數組:");foreach (int num in shuffledArray1){Console.Write(num + " ");}Console.WriteLine();// 方法二:使用 List 和 Random 類System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>(array);int[] shuffledArray2 = new int[list.Count];for (int i = 0; i < shuffledArray2.Length; i++){int index = random.Next(list.Count);shuffledArray2[i] = list[index];list.RemoveAt(index);}Console.WriteLine("方法二亂序后的數組:");foreach (int num in shuffledArray2){Console.Write(num + " ");}Console.WriteLine();}
}
練習 23:一維數組奇數偶數分區
改編題目:編寫一個 C# 程序,創建一個包含 20 個元素的一維數組,使用隨機數(范圍 1 - 100)初始化數組。然后將數組中的奇數元素移到數組左邊,偶數元素移到數組右邊,并輸出分區后的數組。
using System;class OddEvenPartition
{static void Main(){int[] array = new int[20];Random random = new Random();// 用隨機數初始化數組for (int i = 0; i < array.Length; i++){array[i] = random.Next(1, 101);}// 分區操作int left = 0;int right = array.Length - 1;while (left < right){while (left < right && array[left] % 2 != 0){left++;}while (left < right && array[right] % 2 == 0){right--;}if (left < right){int temp = array[left];array[left] = array[right];array[right] = temp;}}// 輸出分區后的數組Console.WriteLine("分區后的數組:");foreach (int num in array){Console.Write(num + " ");}Console.WriteLine();}
}
練習 24:數組排序(選擇、冒泡、插入)
改編題目:編寫一個 C# 程序,創建一個包含 10 個隨機整數(范圍 1 - 100)的數組,分別使用選擇排序、冒泡排序和插入排序算法對數組進行排序,并輸出排序后的數組。
using System;class ArraySorting
{static void Main(){int[] array = new int[10];Random random = new Random();// 用隨機數初始化數組for (int i = 0; i < array.Length; i++){array[i] = random.Next(1, 101);}// 選擇排序int[] selectionSorted = (int[])array.Clone();for (int i = 0; i < selectionSorted.Length - 1; i++){int minIndex = i;for (int j = i + 1; j < selectionSorted.Length; j++){if (selectionSorted[j] < selectionSorted[minIndex]){minIndex = j;}}int temp = selectionSorted[i];selectionSorted[i] = selectionSorted[minIndex];selectionSorted[minIndex] = temp;}Console.WriteLine("選擇排序后的數組:");foreach (int num in selectionSorted){Console.Write(num + " ");}Console.WriteLine();// 冒泡排序int[] bubbleSorted = (int[])array.Clone();for (int i = 0; i < bubbleSorted.Length - 1; i++){for (int j = 0; j < bubbleSorted.Length - i - 1; j++){if (bubbleSorted[j] > bubbleSorted[j + 1]){int temp = bubbleSorted[j];bubbleSorted[j] = bubbleSorted[j + 1];bubbleSorted[j + 1] = temp;}}}Console.WriteLine("冒泡排序后的數組:");foreach (int num in bubbleSorted){Console.Write(num + " ");}Console.WriteLine();// 插入排序int[] insertionSorted = (int[])array.Clone();for (int i = 1; i < insertionSorted.Length; i++){int key = insertionSorted[i];int j = i - 1;while (j >= 0 && insertionSorted[j] > key){insertionSorted[j + 1] = insertionSorted[j];j--;}insertionSorted[j + 1] = key;}Console.WriteLine("插入排序后的數組:");foreach (int num in insertionSorted){Console.Write(num + " ");}Console.WriteLine();}
}
練習 25:二維數組(2 * 3)賦值(兩種方法,單循環、嵌套循環)
改編題目:編寫一個 C# 程序,創建一個 2 行 3 列的二維整數數組,分別使用單循環和嵌套循環的方法為數組元素賦值,賦值規則為從 1 開始依次遞增,并輸出數組元素。
using System;class TwoDimensionalArrayAssignment
{static void Main(){int[,] array = new int[2, 3];// 方法一:單循環賦值int index = 1;for (int i = 0; i < array.GetLength(0); i++){for (int j = 0; j < array.GetLength(1); j++){array[i, j] = index++;}}Console.WriteLine("單循環賦值后的數組:");for (int i = 0; i < array.GetLength(0); i++){for (int j = 0; j < array.GetLength(1); j++){Console.Write(array[i, j] + " ");}Console.WriteLine();}// 方法二:嵌套循環賦值index = 1;for (int i = 0; i < array.GetLength(0); i++){for (int j = 0; j < array.GetLength(1); j++){array[i, j] = index++;}}Console.WriteLine("嵌套循環賦值后的數組:");for (int i = 0; i < array.GetLength(0); i++){for (int j = 0; j < array.GetLength(1); j++){Console.Write(array[i, j] + " ");}Console.WriteLine();}}
}
練習 26:數組內容拷貝
改編題目:編寫一個 C# 程序,有一個 2 行 3 列的二維數組?arr1
,其元素值為?{{1, 2, 3}, {4, 5, 6}}
,將該數組的元素按順序拷貝到一個 3 行 2 列的二維數組?arr2
?中,并輸出?arr2
?的元素。
using System;class ArrayCopying
{static void Main(){int[,] arr1 = { { 1, 2, 3 }, { 4, 5, 6 } };int[,] arr2 = new int[3, 2];int row = 0;int col = 0;for (int i = 0; i < arr1.GetLength(0); i++){for (int j = 0; j < arr1.GetLength(1); j++){arr2[row, col] = arr1[i, j];col++;if (col == arr2.GetLength(1)){col = 0;row++;}}}Console.WriteLine("拷貝后的數組 arr2:");for (int i = 0; i < arr2.GetLength(0); i++){for (int j = 0; j < arr2.GetLength(1); j++){Console.Write(arr2[i, j] + " ");}Console.WriteLine();}}
}
練習 28:初始化特定數組(兩種方法)
改編題目:在 C# 中,使用兩種不同的方法初始化一個 9 行 9 列的二維數組,數組元素按照如下規律排列:
1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 1
1 2 3 3 3 3 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 3 3 3 3 2 1
1 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1
using System;class SpecialArrayInitialization
{static void Main(){int[,] array = new int[9, 9];// 方法一:嵌套循環手動賦值for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){int min = Math.Min(i, j);min = Math.Min(min, 8 - i);min = Math.Min(min, 8 - j);array[i, j] = min + 1;}}Console.WriteLine("方法一初始化后的數組:");for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){Console.Write(array[i, j] + " ");}Console.WriteLine();}// 方法二:分層賦值int[,] array2 = new int[9, 9];for (int layer = 0; layer < 5; layer++){for (int i = layer; i < 9 - layer; i++){for (int j = layer; j < 9 - layer; j++){array2[i, j] = layer + 1;}}}Console.WriteLine("方法二初始化后的數組:");for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){Console.Write(array2[i, j] + " ");}Console.WriteLine();}}
}
練習 29:輸出楊輝三角(二維數組法)n 層
改編題目
編寫一個 C# 程序,讓用戶輸入一個正整數?n
,然后使用二維數組的方法輸出?n
?層的楊輝三角。
using System;class PascalTriangle
{static void Main(){Console.Write("請輸入要輸出的楊輝三角的層數: ");int n = int.Parse(Console.ReadLine());int[,] triangle = new int[n, n];for (int i = 0; i < n; i++){for (int j = 0; j <= i; j++){if (j == 0 || j == i){triangle[i, j] = 1;}else{triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j];}}}Console.WriteLine("楊輝三角:");for (int i = 0; i < n; i++){for (int k = 0; k < n - i - 1; k++){Console.Write(" ");}for (int j = 0; j <= i; j++){Console.Write(triangle[i, j].ToString().PadLeft(4));}Console.WriteLine();