class Program { static void Main(string[] args) { int[] arr = { 12, 34, 6, 22, 8 }; var arry = ConvertSum(arr); int[] num = { 1, 2, 3, 4, 5, 6 }; SetNum(num); SetNum(); } //冒泡排序,目的:按从小到大排序,比较相邻两个元素,如果左边元素大于右边元素,则交换 public static int[] ConvertSum(int[] arry) { int temp = -1; for (int i = arry.Length; i > 0; i--) { for (int j = 0; j < i - 1; j++) { if (arry[j] > arry[j + 1]) { temp = arry[j]; arry[j] = arry[j + 1]; arry[j + 1] = temp; } } } return arry; } //1、要求是用OO的思想处理20位以内整数的乘法(解释器模式) public static void SetNum(int[] num) { var temp = 0; for (int i = 0; i < num.Length; i++) { if (i < num.Length - 1) { if (temp == 0) temp += num[i] * num[i + 1]; else temp += temp * num[i]; } } Console.WriteLine(temp); } //2、11个9 乘以 11个9 等于9999.........01 写个算法的 /// <summary> // 125 //x 11 //------- // 125 // 125 //-------- // 1375 //将乘数每位与乘数相乘,获取到一个m*n的矩阵 //将矩阵错位相加,得到输出数组 //对输出中数组进行进位运算 /// </summary> /// <param name="num1"></param> /// <param name="num2"></param> public static void SetNum(string num1 = "99999999999", string num2 = "99999999999") { var input1 = num1.Select(i => (int)(i - ‘0‘)); var input2 = num2.Select(i => (int)(i - ‘0‘)); //把num1依次和num2每位相乘,获取到一个m*n的矩阵 var rectangel = input2.Select(i => input1.Select(j => j * i).ToArray()).ToArray(); //将矩阵错位相加,得到输出数组 var result = new int[num1.Length + num2.Length]; for (int row = 0; row < num2.Length; row++) { for (int column = 0; column < num1.Length; column++) { result[column + row + 1] += rectangel[row][column]; } } //对输出中数组进行进位运算 for (int i = result.Length - 1; i >= 0; i--) { if (result[i] < 10) continue; result[i - 1] += result[i] / 10; result[i] %= 10; } Console.WriteLine(new string(result.Select(i => (char)(i += ‘0‘)).ToArray())); } }
原文:http://www.cnblogs.com/ingstyle/p/6394995.html