练习 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();