Notice: Undefined index: HTTP_ACCEPT_ENCODING in C:\server\web\www.showyounger.com\s-system.php on line 2
BigNumMult | 小样儿(ShowYounger)
小样儿,顶呱呱!!![http://www.showyounger.com]
- public class BigNumMult {
-
- public static void main(String[] args) {
-
- String x = "30004343435343434343534543526566456545454433454646544432332423424234954352900";
- String y = "535353212313242423435456423423434534624267768899089786756564577567535353";
-
-
-
- String z = mult(x, y);
-
- System.out.println("x=" + x);
- System.out.println("y=" + y);
- System.out.println("x*y=" + z);
- }
-
- private static String mult(String x, String y) {
-
- return mult(x.toCharArray(), y.toCharArray());
- }
-
- private static String mult(char[] x, char[] y) {
-
- int[] xi = new int[x.length];
-
- for (int i = 0; i < x.length; i++) {
- xi[i] = Integer.valueOf(Character.toString(x[i]));
- }
-
- int[] yi = new int[y.length];
- for (int i = 0; i < y.length; i++) {
- yi[i] = Integer.valueOf(Character.toString(y[i]));
- }
-
- return mult(xi, yi);
- }
-
- private static String mult(int[] x, int[] y) {
-
- int[] z = new int[x.length + y.length];
-
- for (int i = 0; i < x.length; i++) {
- if(x[i] == 0) {
- continue;
- }
- for (int j = 0; j < y.length; j++) {
- if(y[j] == 0) {
- continue;
- }
-
- z[x.length - 1 - i + y.length - 1 - j] += x[i] * y[j];
- z = carry(z, x.length - 1 - i + y.length - 1 - j);
- }
- }
-
- StringBuffer sb = new StringBuffer();
-
- for(int i : z) {
- sb.append(i);
- }
-
- String result = sb.reverse().toString().replaceFirst("^0*", "");
- if(result.length() == 0) {
- result = "0";
- }
-
- return result;
- }
-
- private static int[] carry(int[] z, int i) {
-
- int temp = z[i];
- if(temp >= 10) {
- z[i] = temp % 10;
- z[i + 1] += temp / 10;
- z = carry(z, i + 1);
- }
-
- return z;
- }
-
- }