1、 C#编程习题 以下有18道习题(当然,这并非全部的习题,最后应该会多增加2题,共20题),对于这20道题目,要求大家每道题目都能够做到看到题目,不看其他参考材料的情况下,能够实现出来。期末考将会从中抽取三道题目(分数占60分),而期末考其实是要出三份试卷的。所以总的需要从这份里面抽9道题目。也就是这20题中有9题是肯定会出现在试卷中的。 对于本科目最后成绩的说明: 总评成绩=平时成绩*50%+期末成绩*50% 平时成绩与期末成绩都是100分制的。 平时成绩=出勤课堂纪律情况*40%+作业完成情况*60% 对于作业完成情况的说明: 对于之前的作业,不管是否有提交,我都不再追究。
2、我只要求从发这份习题给你们开始,每周要按顺序完成如下的5道题目,把每道题目都自己理解完实现出来之后再交上来。 我知道要把这20题都弄懂,不是一件容易的事情,但不是不可能。 第一, 我会提供一些材料,大家可以先把这些材料看一遍,大体有个印象,当遇到问题的时候,也要学会查找对应材料来解决问题。 第二, 下面的每道题目,我都将会给出参考答案。当然是以截图的方式。也会在关键代码附上相应的注释。 还有一句话,希望能与大家共勉。“学习,从来都是一个自觉的过程。遇上一个好老师,会让你少走很多弯路,但不是替代你学习。遇上一个不够好的老师,或许会多走些弯路,但不代表你就学不好。事在人为,只要自己有心,外
3、界条件再差也是可以攻克的;若是无心,外界条件再好,也会成为你的借口。” 最后,再回到我们这门课的现实。能否取得好成绩,能否不挂科,取决于你,而不取决于我。规则我都已经说清楚了,最后结果如何我说了不算,你的实际行动和实际成绩说了算。 1、 编写一个函数,函数名为Max,该函数用来获取给定三个整数的最大值。在Main函数中实现从屏幕中读取三个整数,然后输出这三个整数中最大的那个数的功能。 namespace _1 { class Program { static void Main(string[] args) {
4、 Console.WriteLine("请输入3个数:按回车结束"); int a, b, c, temp; a = int.Parse(Console.ReadLine());//定义一个输入值a; b = int.Parse(Console.ReadLine());//定义一个输入值b; c = int.Parse(Console.ReadLine());//定义一个输入值c; temp = Max(a, b, c);
5、 Console.WriteLine("a={0},b={1},c={2},三个数中最大值为{3}", a, b, c, temp); } //074 陈振华 static int Max(int a, int b, int c) { int temp = a;//把a的值付给temp; if (temp < b) { temp = b; } if (temp <
6、 c) { temp = c; } return temp; } } } 2、 编写一个函数,函数名为PrintTriangle,用来打印n阶的正三角形。并在Main函数中实现从屏幕中输入n的值,然后根据给定的n的值打印出相应的正三角形。当n=7时, * *** ***** ******* ********* *********** ************* 当n=5时, *
7、 *** ***** ******* ********* 代码: namespace _1 { class Program { static void Main(string[] args) { int a; Console.WriteLine("请输入你想要打印出的三角形的行数"); a = int.Parse(Console.ReadLine()); PrintTriangle(a); }
8、 //074 陈振华 static void PrintTriangle(int a) { for (int i = 1; i <=a; i++) { for (int j = i; j 0; x
9、) { Console.Write("*"); } Console.WriteLine(""); } } } } 3、 输入某年某月某日,判断这一天是这一年的第几天?。要求:需写一个函数,给定年月日,求的该天处于该年的第几天。然后在Main函数中测试。 代码: namespace _1 { class Program { static void
10、 Main(string[] args) { int year, month, day; Console.WriteLine("请输入年份"); year = int.Parse(Console.ReadLine()); Console.WriteLine("请输入月份"); month = int.Parse(Console.ReadLine()); Console.WriteLine("请输入日期");
11、 day = int.Parse(Console.ReadLine()); Console.WriteLine("{0}年{1}月{2}日处于该年的第{3}天", year, month, day, Cs(year, month, day)); } static int Cs(int year, int month, int day) { int sum = 0; for (int i = 1; i < month; i++)
12、 { if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) { sum += 31; } if (i == 4 || i == 6 || i == 9 || i == 11) { sum += 30; }
13、 if (i == 2) { sum += 29; } } if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month > 2) sum = sum - 1; return sum; } } } 4、 给定一个大于三的奇数n,打印出相应的三
14、角形。 如n=7时的形状如下: * *** ***** ******* ***** *** * 代码; class Program { static void Main(string[] args) { int a; Console.WriteLine("请输入你想要打印出的三角形的行数,该数必须是大于三的奇数"); a = int.Parse(Console.ReadLine()); if (a % 2 == 1 &
15、 a >= 3)
{
PrintTriangle(a);
}
else
Console.WriteLine("该数不是大于三的奇数");
}
///
16、a) { for (int i = 1; i <= a / 2 + 1; i++) { Console.Write("*"); for (int x = 1; x < i; x++) { Console.Write("**"); } Console.WriteLine(""); }
17、 for (int i = a / 2; i > 0; i--) { Console.Write("*"); for (int x = i - 1; x > 0; x--) { Console.Write("**"); } Console.WriteLine(""); } } } }
18、 要求:编写一个函数,给定一个n,打印出相应的三角形,若n不是奇数,则提示说n的值必须为奇数。 5、 给一个正整数,要求: a) 写一个函数求出它是几位数。 b) 写一个函数,分别输出每一位数字。 c) 写一个函数,按逆序输出各位数字,例如原数位123,则输出321. 最后在main函数中测试结果 代码: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int a, r;
19、 Console.WriteLine("请输入一个正整数"); a = int.Parse(Console.ReadLine()); r = C105(a); Console.WriteLine("该数是{0}位数", r); int[] A = Db(a); Shuzhu(A); Cs(A); } static int C105(int a)
20、 { int i = 1; while (a > 10) { a = a / 10; i++; } return i; } static int[] Db(int a) { int count = C105(a); int[] data = new int[count];
21、 while (a > 0) { data[--count] = a % 10; a = a / 10; } return data; } static void Shuzhu(int[] A) { for (int i = 0; i < A.Length; i++) { Console.Wri
22、te("{0} ",A[i]); } Console.WriteLine(); } static void Cs(int[] A) { for (int i = 0; i < A.Length / 2; i++) { int temp = A[i]; A[i] = A[A.Length - i - 1]; A[A.Length - i
23、 1] = temp; } Console.WriteLine("倒叙输出后的结果为"); for (int i = 0; i < A.Length; i++) { Console.Write("{0} ", A[i]); } Console.WriteLine(); } } } 6、 请编写一个函数,计算出从m到n的数的和,比如m=3,n=300,则计算出从
24、3一直加到300的和。并在Main函数中调用测试。 要求: 1) 写个函数采用for语句实现该功能。 2) 写个函数采用while语句实现该功能。 3) 写个函数采用do…while语句实现该功能。 代码: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int a,b; Console.WriteLine("请输入两个正整数分,别按回车结束");
25、 a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); For(a, b); While(a, b); Dowhile(a, b); } static void For(int a, int b) { int sum = 0; for (int i = a; i <= b; i++)
26、 { sum = sum + i; } Console.WriteLine("从{0}到{1}的所有数的和={2}",a,b,sum); } //074 陈振华 static void While(int a, int b) { int sum = 0,i=a; while (i <= b) { sum =
27、sum + i; i++; } Console.WriteLine("从{0}到{1}的所有数的和={2}", a, b, sum); } static void Dowhile(int a, int b) { int sum = 0, i = a; do { sum = sum + i; i++;
28、 } while (i <= b); Console.WriteLine("从{0}到{1}的所有数的和={2}", a, b, sum); } } } 7、 写一个函数,判断给定的一个正整数是否是素数,并在Main函数中测试。 8、 代码: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int m, a;
29、 m = int.Parse(Console.ReadLine()); a = Sushu105(m); if (a == 0) { Console.WriteLine("这个数是素数", a); } else Console.WriteLine("这个数不是是素数", a); } static int Sushu105(int m)
30、 { int a = 0; for (int i = 2; i < m; i++) { if (m % i == 0) { a++; } } return a; } } } 9、 写一个函数,返回给定m~n之间的所有素数。再写一个函数,打印出给定一个数组中的所有元素。并在Main函数
31、中测试。 10、 代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("请输入两个数"); int a, b; a
32、 int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); //bool result = IsSushu(6); //Console.WriteLine(result); int[] wuxinglin = Suzhu(a, b); Console.WriteLine("{0}到{1}之间的所有素数如下", a, b); Daying(wuxinglin);
33、
}
///
34、nt[] wuxinglin = new int[count]; int d = 0; for (int i = a; i <= b; i++) { if (IsSushu(i)) { wuxinglin[d] = i; d++; } } return wuxinglin;
35、 }
///
36、 b; i++)
{
if (IsSushu(i))
{
c++;
}
}
return c;
}
///
37、
///
38、k;
}
}
return result;
}
///
39、ngth; i++) { Console.Write("{0} ", wuxinglin[i]); } } } } 11、 编写一个函数,返回给定一个正整数m的二进制表示。比如给定11,则返回1011.并在Main函数中测试。 12、 代码: namespace _1 { class Program { static void Main(string[] args) { Con
40、sole.WriteLine("请输入一个正整数,按回车键结束"); int a = int.Parse(Console.ReadLine()); Console.WriteLine("输入的数{0}被转化为二进制后的结果为{1}", a, Zhuanhua(a)); } static string Zhuanhua(int a) { StringBuilder result=new StringBuilder(); while (a >
41、 0) { result.Insert(0,a%2); a = a / 2; } return result.ToString(); } } } 13、 编写一个函数,返回给定一个正整数m的n位二进制表示方法,若n小于实际给定数m的二进制位数,则返回其真实位数。 比如m=100,n=3,则返回1100100 比如m=100,n=8,则返回001100100 代码: namespace _1 {
42、 class Program { static void Main(string[] args) { Console.WriteLine("请输入一个正整数,按回车键结束"); int a = int.Parse(Console.ReadLine()); Console.WriteLine("请输入你想返回的二进制的位数"); int b = int.Parse(Console.ReadLine()); stri
43、ng result = Zhuanhua(a, b); Console.WriteLine("输入的数{0}被转化为二进制后{1}位的结果为{2}", a,b,result); } static string Zhuanhua(int a,int b) { StringBuilder result=new StringBuilder(); int count = 0; while (a > 0) {
44、 result.Insert(0,a%2); a = a / 2; count++; } for (int i = count; i <= b; i++) { result.Insert(0, 0); } return result.ToString(); } } } 14、 写一个函数,实现对给定一个整型数
45、组中的每个元素进行逆序重新存放。例如,原来顺序为1,3,6,5,7,。要求改为7,5,6,3,1。并在Main函数中测试。 15、 代码: namespace _074 { class Program { static void Main(string[] args) { Console.WriteLine("请输入一个整形数组中间用逗号隔开如\"10,11,12\""); string a = Console.ReadLine();
46、string[] tempData = a.Split(','); int[] data = new int[tempData.Length]; for (int i = 0; i < tempData.Length; i++) { data[i] = int.Parse(tempData[i]); } data = Shuzhu(data); Console.WriteLine("逆序输出后的数组如下")
47、 Daying(data); } static int[] Shuzhu(int[] data) { int temp; for (int i = 0; i < data.Length/2; i++) { temp = data[i]; data[i] = data[data.Length - i - 1];
48、 data[data.Length - i - 1] = temp; } return data; } static void Daying(int[] data) { for (int i = 0; i < data.Length; i++) { Console.Write("{0},", data[
49、i]); } } } } 16、 写一个函数,给定一个数n,要求返回n行的杨辉三角形。并在Main函数中测试。 17、 代码: namespace _074 { class Program { static void Main(string[] args) { Console.WriteLine("请输入要打印的杨辉三角的行数,按回车结束!"); int n = int.Parse(
50、Console.ReadLine()); Console.WriteLine("{0}行的杨辉三角如下:", n); PrintYangaHuiTriangle(n); } static void PrintYangaHuiTriangle(int n) { int[,] data = new int[n, n]; for (int i = 1; i <= n; i++) { d






