1./*正整数反转*/
int initialData = 0; string finallyData = string.Empty; do { Console.WriteLine("请输入一个整数:"); initialData = int.Parse(Console.ReadLine()); } while (initialData <= 0); while (initialData > 0) { finallyData += (initialData % 10).ToString(); initialData = initialData / 10; } Console.WriteLine(finallyData); Console.ReadKey();
2./*杨辉三角*/
int[,] array = new int[6, 6]; array[0, 0] = 1; for (int i = 1; i < 6; i++) { array[i, 0] = 1; array[i, i] = 1; for (int j = 1; j < i; j++) { array[i, j] = array[i - 1, j - 1] + array[i - 1, j]; } } for (int k = 0; k < 6; k++) { for (int m = 0; m <= k; m++) { Console.Write("{0}\t",array[k,m]); } Console.WriteLine(); }
3./*编程实现求出若干个整数之和为500的连续整数的所有组合(例如98 99 100 101 102)*/
for (int i = 1; i <= 250; i++) { int j = i;//初始化第一个连续整数(因为是连续整数,遍历上限是250) int s = i; while (s < 500) { j++; s = s + j; } if ((j != i) && (s == 500)) { for (int k = i; k <= j; k++) { Console.Write("{0} ", k); } Console.WriteLine(); } }
4./*产生一个int数组,长度为100,并向其中随机插入1--100,要求不重复*/
int[] intArr = new int[100]; ArrayList myList = new ArrayList(); Random r = new Random(); while (myList.Count < 100) { int num = r.Next(1, 101); if (!myList.Contains(num)) myList.Add(num); } for (int i = 0; i < 100; i++) { intArr[i] = (int)myList[i]; } for (int i = 0; i < intArr.Length; i++) Console.Write(intArr[i] + " ");
5./*用1分,2分,5分的面额,组成1元*/
int x = 0; for (int i = 0; i < 21; i++) { for (int j = 0; j < 51; j++) { for (int k = 0; k < 101; k++) { if ((5 * i + 2 * j + k) == 100) { x++; Console.WriteLine("方法{0}:{1}+{2}+{3}=10元", x, i, j, k); } } } } Console.WriteLine("共有{0}中方法!", x);