1.输出下列的格式的数字
/*
1,5 1,4 2,5 1,3 2,4 3,5 1,2 2,3 3,4 4,5 */
View Code
2./*一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。*/ int[] a = { 1, 2, 3, 4, 5 }; for (int i = 0; i < a.Length - 1; i++) { for (int j = 0; j < i + 1; j++) { Console.WriteLine("(" + a[j] + "," + a[a.Length - 1 - i + j] + ")"); } }
View Code
public static int DealNum(int i) { if (i <= 0) return 0; else if (i > 0 && i <= 2) return 1; else return DealNum(i - 1) + DealNum(i - 2); } 调用: Console.write(DealNum(30));
3.//冒泡排序
View Code
public static int[] MaoPao(int[] Data) { int temp = 0; int length = Data.Length - 1; for (int i = 0; i < length; i++) { for (int j = length; j > i; j--) { if (Data[j] > Data[j - 1]) { temp = Data[j]; Data[j] = Data[j - 1]; Data[j - 1] = temp; } } } return Data; } 调用:int[] array = new int[] { 1, 4, 2, 3, 5 }; int[] newdata = MaoPao(array); for (int i = 0; i < newdata.Length; i++) { Console.Write(newdata[i]); }
4. /* 求一下表达式的值 1-2+3-4+......+m */
View Code
public static int DealNum(int m) { int sum =0; bool flag=true; for(int i=1;i<=m;i++) { if(flag) sum+=i; else sum-=i; flag=!flag; } return sum; }