java大数乘法分治_用分治法实现大数乘法,加法,减法(java实现)

1 packagecom.kyy.sf;2

3 public classBigInteger {4

5 publicBigInteger() {6

7 }8

9 //基本思想是把多项式A(x)与B(x)写成10 //A(x)=a*x^m+b11 //B(x)=c*x^m+d12 //其中a,b,c,d为x的多项式。13 //则A(x)*B(x)=(ac)*x^2m+(ad+bc)*x^m+bd14 //由ad+bc=(a+b)(c+d)-ac-bd15 //字符串模拟乘法操作

16

17 public staticString mut(String x, String y) {18 //deep++;//Console.WriteLine("-" + deep + "-");

19 String negative = "";20 //x,y同为正或者同为负

21 if ((x.startsWith("-") && y.startsWith("-"))22 || (!x.startsWith("-") && !y.startsWith("-"))) {23 x = x.replaceAll("-", "");24 y = y.replaceAll("-", "");25 negative = "";26 }//x,y一正一负

27 else if ((x.startsWith("-") && !y.startsWith("-"))28 || (!x.startsWith("-") && y.startsWith("-"))) {29 x = x.replace("-", "");30 y = y.replace("-", "");31 negative = "-";32 }33

34 //如果长度都等于于9,直接相乘,返回就行了。

35 if (x.length() == 1 && y.length() == 1) {36 //计算乘积

37 int tmp = (Integer.parseInt(x) *Integer.parseInt(y));38

39 if (tmp == 0) {40 return tmp + "";41 } else{42 return negative +tmp;43 }44 }45

46 //公式里的abcd

47 String a, b, c, d;48 if (x.length() == 1) {49 a = "0";50 b =x;51 } else{52 if (x.length() % 2 != 0) {53 x = "0" +x;54 }55 a = x.substring(0, x.length() / 2);56 b = x.substring(x.length() / 2);57 }58 if (y.length() == 1) {59 c = "0";60 d =y;61 } else{62 if (y.length() % 2 != 0) {63 y = "0" +y;64 }65 c = y.substring(0, y.length() / 2);66 d = y.substring(y.length() / 2);67 }68 //按最大位数取值,以确定补零数目

69 int n = x.length() >= y.length() ?x.length() : y.length();70

71 String t1, t2, t3;72 //递归调用,根据公式计算出值。

73 String ac =mut(a, c);74 String bd =mut(b, d);75 t1 =mut(sub(a, b), sub(d, c));76 t2 =add(add(t1, ac), bd);77 t3 = add(add(Power10(ac, n), Power10(t2, n / 2)), bd).replaceAll("^0+",78 "");79

80 if (t3 == "")81 return "0";82 return negative +t3;83 }84

85 private staticString add(String x, String y) {86

87 if (x.startsWith("-") && !y.startsWith("-")) {88 return sub(y, x.replaceAll("^-", ""));89 } else if (!x.startsWith("-") && y.startsWith("-")) {90 return sub(x, y.replaceAll("^-", ""));91 } else if (x.startsWith("-") && y.startsWith("-")) {92 return "-" + add(x.replaceAll("^-", ""), y.replaceAll("^-", ""));93 }94

95 if (x.length() >y.length()) {96 y = format(y, x.length(), "0");97 } else{98 x = format(x, y.length(), "0");99 }100 int[] sum = new int[x.length() + 1];101

102 for (int i = x.length() - 1; i >= 0; i--) {103 int tmpsum = Integer.parseInt(x.charAt(i) + "")104 + Integer.parseInt(y.charAt(i) + "") + sum[i + 1];105 if (tmpsum >= 10) {106 sum[i + 1] = tmpsum - 10;107 sum[i] = 1;//表示进位

108 } else{109 sum[i + 1] =tmpsum;110 }111 }112

113 StringBuilder returnvalue = newStringBuilder();114

115 for (inti : sum) {116 returnvalue.append(i);117 }118

119 if (sum[0] == 1) {120

121 returnreturnvalue.toString();122

123 } else{124 return returnvalue.replace(0, 1, "").toString();125 }126

127 }128

129 //字符串模拟减法操作

130 private staticString sub(String x, String y) {131

132 //x是正数,y也是正数

133 int flag =checkBigger(x, y);134

135 if (flag == 0) {136 return "0";137 } else if (flag == -1) {138 String tmp =y;139 y =x;140 x =tmp;141 }142 //保证了x>=y

143 y = format(y, x.length(), "0");//y补0与x对齐

144

145 int[] difference = new int[x.length()];146

147 for (int i = x.length() - 1; i >= 0; i--) {148

149 inttmpdifference;150

151 tmpdifference = Integer.parseInt(x.charAt(i) + "")152 - Integer.parseInt(y.charAt(i) + "") +difference[i];153

154 if (tmpdifference < 0) {155

156 tmpdifference += 10;157 difference[i - 1] = -1;//表示进位

158 }159

160 difference[i] =tmpdifference;161 }162

163 StringBuilder returnvalue = newStringBuilder();164

165 for (inti : difference) {166 returnvalue.append(i);167 }168

169 String rv = returnvalue.toString().replaceAll("^0+", "");170

171 if ("".equals(rv)) {172 return "0";173 }174

175 if (flag == -1) {176 rv = "-" +rv;177 }178

179 returnrv;180 }181

182 //比较大小

183 private static intcheckBigger(String x, String y) {184

185 if (x.length() >y.length()) {186

187 return 1;188

189 } else if (x.length()

191 return -1;192

193 } else{194

195 for (int i = 0; i < x.length(); i++) {196

197 if (x.charAt(i) >y.charAt(i)) {198

199 return 1;200

201 } else if (x.charAt(i)

206 return 0;207 }208 }209

210 //数据前补零

211 private static String format(String str, intlen, String fu) {212

213 len = len -str.length();214

215 for (int i = 0; i < len; i++) {216

217 str = fu +str;218 }219

220 returnstr;221

222 }223

224 //模拟移位

225 public static String Power10(String num, intn) {226

227 for (int i = 0; i < n; i++) {228

229 num += "0";230

231 }232

233 returnnum;234 }235

236 public static voidmain(String[] args) {237

238 String x = "93859048059849086850986804750894758903278473894578397598475984784857487584758094875890475984955624146039530798877974";239 String y = "224343444859408590475847538946";240 System.out.println(mut(x, y));241

242 System.out.println(mut("1111111111", "1111111111"));243

244 }245 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值