现实中,进行数值很大的数据(超过默认数据类型范围)运算时,可以用数组来完成。
例1:利用一维数组完成三位数加法(N位数加法类似;非最简解法,后同)
1 int[] a = {0,9,2,3}, b = {0,7,0,9}, c = new int[4]; 2 int jinwei = 0,t; 3 for (int i = a.Length-1; i >=1; i--) 4 { 5 t = a[i] + b[i]; 6 c[i] = t % 10+jinwei; 7 jinwei = t / 10; 8 } 9 c[0] = jinwei; 10 for (int i = 0; i < a.Length; i++) 11 { 12 Console.Write(a[i]); 13 } 14 Console.Write("+"); 15 for (int i = 0; i < b.Length; i++) 16 { 17 Console.Write(b[i]); 18 } 19 Console.Write("="); 20 for (int i = 0; i < c.Length; i++) 21 { 22 Console.Write(c[i]); 23 } 24 Console.ReadKey();
运行结果:
三位数减法:
1 int[] a = { 4, 5, 6 }, b = { 1, 4, 7 }, c= new int[3]; 2 int jw=0,t; 3 for (int i = a.Length-1; i >=0; i--) 4 { 5 t = a[i] - jw - b[i]; 6 if (t<0) 7 { 8 t += 10; 9 jw = 1; 10 } 11 else 12 { 13 jw = 0; 14 } 15 c[i] = t; 16 } 17 for (int i = 0; i < c.Length; i++) 18 { 19 Console.Write(c[i]); 20 } 21 Console.ReadKey();
结果:
例2:三位数乘法
1 int[] a = { 9, 2, 3 }, b = { 7, 0, 9 }, c = new int[4], d = { 0, 0, 0, 0, 0, 0 }; 2 int jinwei, t; 3 for (int i = 0; i < b.Length; i++) 4 { 5 jinwei = 0; 6 for (int j = 0; j < c.Length; j++) 7 { 8 c[j] = 0; 9 } 10 for (int j = 0; j < a.Length; j++) 11 { 12 t = a[a.Length - 1 - j] * b[b.Length - 1 - i]; 13 c[c.Length - 1 - j] += t % 10; 14 jinwei = t / 10 + c[c.Length - 1 - j]/10; 15 c[c.Length - 1 - j] %= 10; 16 if (jinwei > 0) 17 { 18 c[c.Length - 1 - j-1] +=jinwei; 19 } 20 } 21 //测试中间累加的数字 22 //for (int j = 0; j < c.Length; j++) 23 //{ 24 // Console.Write(c[j]); 25 //} 26 //Console.WriteLine(); 27 //测试结束 28 jinwei = 0; 29 for (int j = 0; j < c.Length; j++) 30 { 31 t = d[d.Length - 1 - i - j] + c[c.Length - 1 - j]; 32 d[d.Length - 1 - i - j] = t % 10; 33 jinwei = t / 10; 34 jinwei += d[d.Length - 1 - i - j] / 10; 35 d[d.Length - 1 - i - j] %= 10; 36 if (jinwei > 0) 37 { 38 d[d.Length - 1 - i - j - 1] += jinwei; 39 } 40 } 41 } 42 for (int i = 0; i < a.Length; i++) 43 { 44 Console.Write(a[i]); 45 } 46 Console.Write("*"); 47 for (int i = 0; i < b.Length; i++) 48 { 49 Console.Write(b[i]); 50 } 51 Console.Write("="); 52 for (int i = 0; i < d.Length; i++) 53 { 54 Console.Write(d[i]); 55 } 56 Console.ReadKey();
运行结果:
利用二位数组完成,会简单一些:
1 int[] a = { 9, 4, 7 }, b = { 3, 6, 9 }; 2 //M位数和N位数相乘,最多有M+N位数,N位中间结果 3 int[,] c = new int[b.Length + 1, a.Length + b.Length]; 4 //初始化 5 for (int i = 0; i < c.GetLength(0); i++) 6 { 7 for (int j = 0; j < c.GetLength(1); j++) 8 { 9 c[i, j] = 0; 10 } 11 } 12 //分位乘 13 for (int i = b.Length - 1; i >= 0; i--) 14 { 15 for (int j = a.Length - 1; j >= 0; j--) 16 { 17 c[i, c.GetLength(1) - 1 - (b.Length - 1 - i) - (a.Length - 1 - j)] = a[j] * b[i]; 18 } 19 } 20 //中间结果累加 21 for (int i = 0; i <= c.GetLength(0) - 2; i++) 22 { 23 for (int j = c.GetLength(1) - 1; j >= 0; j--) 24 { 25 c[c.GetLength(0) - 1, j] += c[i, j]; 26 } 27 } 28 //对累加结果的进位部分进行处理 29 for (int i = c.GetLength(1) - 1; i >= 0; i--) 30 { 31 if (c[c.GetLength(0) - 1, i] >= 10) 32 { 33 c[c.GetLength(0) - 1, i - 1] += c[c.GetLength(0) - 1, i] / 10; 34 c[c.GetLength(0) - 1, i] %= 10; 35 } 36 } 37 //输出结果 38 for (int i = 0; i < c.GetLength(1); i++) 39 { 40 Console.Write(c[c.GetLength(0) - 1, i]); 41 } 42 Console.ReadKey();
运行结果: