所以我一直在为这个任务而苦苦挣扎,在这里我必须编写一个Java代码来将2个二进制数乘以一个String。我不知道如何将它们相乘并将它们保存在单独的字符串中,以便在末尾添加它们。有人可能会给我一些提示,让我知道如何开始或类似的事情? 我必须使用给定的字符串进行乘法运算,并且无法将它们转换为整数。我知道如何在纸上进行乘法,但不知道如何将其放入代码中。 到目前为止,这就是我的代码。
public static String multiply(String binary1, String binary2) {
String b1 = new StringBuilder(binary1).reverse().toString();
String b2 = new StringBuilder(binary2).reverse().toString();
int[] m = new int[binary1.length()+binary2.length()];
for (int i = 0; i < binary1.length(); i++) {
for (int j = 0; j < binary2.length(); j++) {
m[i+j] += (b1.charAt(i)-'0')*(b2.charAt(j)-'0');
}
}
StringBuilder sb = new StringBuilder();
for(int i=0; i < m.length; i++) {
int mod = m[i]%10;
int carry = m[i]/10;
if (i+1 < m.length) {
m[i + 1] = m[i + 1] + carry;
}
sb.insert(0, mod);
}
// remove front zeros
while (sb.charAt(0) == '0' && sb.length() > 1) {
sb.deleteCharAt(0);
}
return sb.toString();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("1. Factor: ");
String input1 = scan.next("(0|1)*");
System.out.print("2. Factor: ");
String input2 = scan.next("(0|1)*");
scan.close();
System.out.println("Result: " + multiply(input1, input2));
}
答案 0 :(得分:0)
看看是否有帮助:
// your 2 binary strings
String aBin = "100001";
String bBin = "111110";
// convert them to int
int a = Integer.parseInt(aBin, 2);
int b = Integer.parseInt(bBin, 2);
// Do the math you want and store the result on 'r'
int r = a * b;
// If you want, convert the result to the binary string format
String rBin = Integer.toBinaryString(r);
编辑1:
如果数字不适合int
,请改用BigInteger
:
// your 2 binary strings
String aBin = "100001";
String bBin = "111110";
// convert them to BigInteger
BigInteger a = new BigInteger(aBin, 2);
BigInteger b = new BigInteger(bBin, 2);
// Do the math you want and store the result on 'r'
BigInteger r = a.multiply(b);
// If you want, convert the result to the binary string format
String rBin = r.toString(2);
编辑2 :
了解OP的要求后:
public static String multiply(String binary1, String binary2) {
try {
binary1 = binary1.substring(binary1.indexOf('1'));
binary2 = binary2.substring(binary2.indexOf('1'));
} catch (StringIndexOutOfBoundsException e) {// some number is 0
return "0";
}
// 1st step: multiply each bit of binary2 by binary1
String zero = IntStream.range(0, binary1.length()).mapToObj(v -> "0").collect(Collectors.joining());
StringBuilder lpad = new StringBuilder(IntStream.range(0, binary2.length() - 1).mapToObj(v -> "0").collect(Collectors.joining()));
StringBuilder rpad = new StringBuilder();
String[] prod = new String[binary2.length()];
for (int i = binary2.length() - 1, j = 0; i >= 0; i--, j++) {
prod[j] = lpad.toString()
.concat(binary2.charAt(i) == '1' ? binary1 : zero)
.concat(rpad.toString());
if (lpad.length() > 0) {
lpad.deleteCharAt(0);
rpad.append('0');
}
}
// 2nd step: sum the products
String result = prod[0];
for (int i = 1; i < prod.length; i++) {
StringBuilder sum = new StringBuilder();
int[] carry = new int[binary1.length() + binary2.length()];
for (int j = binary2.length() + binary1.length() - 2; j >= 0; j--) {
int r = result.charAt(j) - '0' + prod[i].charAt(j) - '0';
if (r > 1) {
sum.append(carry[j + 1]);
carry[j] = 1;
} else if ((r += carry[j + 1]) > 1) {
sum.append('0');
carry[j] = 1;
} else {
sum.append(r);
}
}
if (carry[0] == 1) {
sum.append('1');
}
result = sum.reverse().toString();
}
return result;
}
答案 1 :(得分:0)
正如我上面提到的,解决方案与小数乘法相同,因此您可以简单地获得此solution。下面是leetcode的解决方案代码。我认为使用转换为int或使用BigInteger之类的东西并不是您要执行的操作。
public String multiply(String num1, String num2) {
int m = num1.length(), n = num2.length();
int[] pos = new int[m + n];
for(int i = m - 1; i >= 0; i--) {
for(int j = n - 1; j >= 0; j--) {
int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
int p1 = i + j, p2 = i + j + 1;
int sum = mul + pos[p2];
pos[p1] += sum / 10;
pos[p2] = (sum) % 10;
}
}
StringBuilder sb = new StringBuilder();
for(int p : pos) if(!(sb.length() == 0 && p == 0)) sb.append(p);
return sb.length() == 0 ? "0" : sb.toString();
}
P.S。为了了解纸上的乘法过程,请检查this。从这里您可以得出编程解决方案