现在开始有了C#这门课,老师用的教材是《C#实用教程·第二版》,本篇是把这本书的第二章的编程题写了出来。
例1:
using System; namespace First_1 { class Program { static void Main(string[] args) { test0(); Console.ReadLine(); } public static void test0() { int[] num = new int[30]; num[0] = num[1] = 1; for (int i = 2; i < 30; i++) { num[i] = num[i - 1] + num[i - 2]; } for (int i = 0; i < 30; i++) { Console.Write(num[i].ToString().PadRight(10)); if (0 == (i + 1) % 5) { Console.WriteLine(); } } } } }
输出结果为:
例2:
using System; namespace First_2 { class Program { static void Main(string[] args) { double y = 0.0d; test0(y); Console.ReadLine(); } public static void test0(double y) { int number = 0; Console.WriteLine("请输入一个整数:"); string input = Console.ReadLine(); int n = Convert.ToInt32(input); for (int i = 1; i <= n; i++) { number += fac(i); } y = (double)number; Console.WriteLine("n:{0}", n); Console.WriteLine("y:{0:F2}", y); } public static int fac(int t) { int num = 1; for (int i = t; i > 0; i--) { num *= i; } return num; } } }
输出结果为:
例3:
using System; namespace First_3 { class Program { static void Main(string[] args) { Console.WriteLine("水仙花数有:"); test0(); Console.ReadLine(); } public static void test0() { const int N = 3; int[] num = new int[N]; int sum = 0; for (int i = 100; i < 1000; i++) { sum = 0; num[0] = i / 100; num[1] = (i - num[0] * 100) / 10; num[2] = i % 10; for (int j = 0; j < N; j++) { sum += num[j] * num[j] * num[j]; } if (sum == i) { Console.WriteLine(i); } } } } }
输出结果为:
例4:
using System; namespace First_4 { class Program { const int A = 4; static void Main(string[] args) { test0(); Console.ReadLine(); } public static void test0() { int[] n = new int[A]; Console.WriteLine("请输入一个四位整数:"); string input = Console.ReadLine(); int num = Convert.ToInt32(input); for (int i = 0; i < A; i++) { n[i] = num % 10; num /= 10; Console.Write(n[i]); } } } }
输出结果为:
例5:
using System; namespace First_5 { class Program { const int N = 1000; static void Main(string[] args) { test0(N); Console.ReadLine(); } public static void test0(int n) { double d = 1.0d; for (int i = 1; i <= n; i++) { d *= (2 * i) * 1.0 / (2 * i - 1) * ((2 * i) * 1.0 / (2 * i + 1)); } Console.WriteLine("π的近似值为:{0:F10}", d * 2); } } }
输出结果为:
例6:
using System; namespace First_6 { class Program { static void Main(string[] args) { test0(); Console.ReadLine(); } public static void test0() { string input = Console.ReadLine(); int num = Convert.ToInt32(input); Console.WriteLine("0x{0:X}", num); } } }
输出结果为:
例7:
using System; namespace First_7 { class Program { static void Main(string[] args) { Console.WriteLine("请输入x的值:"); string input_x = Console.ReadLine(); double x = Convert.ToDouble(input_x); Console.WriteLine("请输入n的值:"); string input_n = Console.ReadLine(); int n = Convert.ToInt32(input_n); test1(n, x); Console.ReadLine(); } public static double test0(int n, double x) { if (n == 0) return 1; if (n == 1) return 2 * x; else return 2 * x * test0(n - 1, x) - 2 * (n - 1) * test0(n - 2, x); } public static void test1(int n, double x) { for (int i = 0; i <= n; i++) { Console.WriteLine(test0(i, x)); } } } }
输出结果为:
例8:
using System; namespace First_8 { class Program { static void Main(string[] args) { test0(); Console.ReadLine(); } const int N = 10; public static void test0() { int[] a = {7, 3, 8, 34, 12, 1, 0, 3, 5, 8}; /* * 这是生成随机数数组 Random rand = new Random(); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[0] = rand.Next(1, 100); } */ int max = a[0]; int subs = 0; for (int i = 1; i < N; i++) { if (max < a[i]) { max = a[i]; subs = i; } } Console.WriteLine("数组元素分别是:"); for (int i = 0; i < N; i++) { Console.Write("{0, 3}", a[i]); } Console.WriteLine("\n\n最大元素为:{0}\n其下标为:{1}", max, subs); } } }
输出结果为:
例9:
using System; namespace First_9 { class Program { static void Main(string[] args) { test0(); Console.ReadLine(); } public static void test0() { string s = Console.ReadLine(); for (int i = 0; i <= s.Length / 2; i++) { if (s[i] != s[s.Length - i - 1]) { goto ASD; } else if (i == s.Length / 2) { Console.WriteLine("该数是回文!"); return; } } ASD: Console.WriteLine("该数不是回文!"); } } }
输出结果为:
例10:
using System; namespace First_10 { class Program { static void Main(string[] args) { int pos = 0; int neg = 0; int number = 0; test0(pos, neg, number); Console.ReadLine(); } const int N = 1000; public static void test0(int pos, int neg, int number) { int sum = 0; string input; int[] num = new int[N]; for (int i = 0; i < N; i++) { input = Console.ReadLine(); num[i] = Convert.ToInt32(input); if (0 == num[i]) { number = i; break; } } for (int j = 0; j < number; j++) { sum += num[j]; if (num[j] > 0) { pos++; } else { neg++; } } Console.WriteLine("这a组数的平均值为:{0}", sum * 1.0 / number); Console.WriteLine("这组数中正数的个数为a{0}", pos); Console.WriteLine("这组数中负数的个数为a{0}", neg); } } }
输出结果为:
例11:
using System; namespace First_11 { class Program { static void Main(string[] args) { int[,] num = new int[4,4] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; int sum = 0; test0(num, sum); Console.ReadLine(); } public static void test0(int[,] num, int sum) { for (int i = 0; i < num.GetLength(0); i++) { sum += num[i, i]; sum += num[num.GetLength(0) - 1 - i, i]; } Console.WriteLine("矩阵元素有:"); for (int j = 0; j < num.GetLength(1); j++) { for (int k = 0; k < num.GetLength(0); k++) { Console.Write("{0, 4}", num[j, k]); } Console.WriteLine(); } Console.WriteLine("两对角线元素之和为:{0}", sum); } } }
输出结果为:
第十二题
暂时没写出来,回头想出来会附上
如有错误,欢迎指出
所有程序都是在Visual Studio 2010旗舰版下运行通过