gpt4 book ai didi

java - 乘以没有大整数的大整数

转载 作者:行者123 更新时间:2023-12-04 07:16:11 26 4
gpt4 key购买 nike

嘿,所以我想知道如何在不导入它们的情况下乘以大整数,我几乎拥有所有这些。唯一不起作用的是当与 -digit 相乘时,输出仍然是 +。任何帮助表示赞赏。 IE。 -20*4=80

    Scanner scan = new Scanner(System.in);
System.out.println("Type the first number:");
String x = scan.nextLine();

System.out.println("Type the second number:");
String y = scan.nextLine();

if (x.charAt(0) == '-' && y.charAt(0) != '-') {
x = x.substring(1);
}
else if (x.charAt(0) != '-' && y.charAt(0) == '-') {
y = y.substring(1);
}
else if (x.charAt(0) == '-' && y.charAt(0) == '-') {
x = x.substring(1);
y = y.substring(1);
}

String s1 = new StringBuffer(x).reverse().toString();
String s2 = new StringBuffer(y).reverse().toString();

int[] m = new int[s1.length() + s2.length()];

for (int i=0; i<s1.length(); i++) {
for (int j=0; j<s2.length(); j++) {
m[i+j] = m[i+j] + (s1.charAt(i) - '0') * (s2.charAt(j) - '0');
}
}

String product = new String();

for (int i=0; i<m.length; i++) {
int digit = m[i] % 10;
int carry = m[i] / 10;
if (i+1 < m.length) {
m[i+1] = m[i+1] + carry;
}
product = digit + product;
}

while (product.length() > 1 && product.charAt(0) == '0') {
product = product.substring(1);
}

if (x.charAt(0) == '-' && y.charAt(0) != '-') {
product = new StringBuffer(product).insert(0, '-').toString();
}
else if (x.charAt(0) != '-' && y.charAt(0) == '-') {
product = new StringBuffer(product).insert(0, '-').toString();
}
else if (x.charAt(0) == '-' && y.charAt(0) == '-') {
product = product;
}
System.out.println("The multiplication of\n" + x + " \nand\n" + y + " " + "\nis\n" + product);

scan.close();
}
}

最佳答案

您要从此处的数字中删除负号:

if (x.charAt(0) == '-' && y.charAt(0) != '-') {
x = x.substring(1);
}
else if (x.charAt(0) != '-' && y.charAt(0) == '-') {
y = y.substring(1);
}
所以在这些行之后你的 xy变量不再包含负号。
所以当你在这里检查它接近尾声时:
if (x.charAt(0) == '-' && y.charAt(0) != '-') {
product = new StringBuffer(product).insert(0, '-').toString();
}
else if (x.charAt(0) != '-' && y.charAt(0) == '-') {
product = new StringBuffer(product).insert(0, '-').toString();
}
else if (x.charAt(0) == '-' && y.charAt(0) == '-') {
product = product;
}
它永远不会进入任何条件。
最后调试这个的一个好方法是在你认为它应该进入的条件内设置一个断点,看看它是否被击中。或者在条件之前断点并检查变量以确保它们是您期望的那样。你也可以暂时在那里抛出一些 println 语句只是为了说“我进入了这个条件”。
我建议进行的调整是在剥离负数之前先确定每个数字是否为负数,以便您以后可以在逻辑中使用它。
这是应该完成您想要的调整。我使用整数而不是 boolean 值来检查是否稍后更容易应用负号(即 isFirstNumberNegative + isSecondNumberNegative == 1 )
    Scanner scan = new Scanner(System.in);
System.out.println("Type the first number:");
String x = scan.nextLine();

System.out.println("Type the second number:");
String y = scan.nextLine();

// hold onto which numbers are negative
Integer isFirstNumberNegative = x.charAt(0) == '-' ? 1 : 0;
Integer isSecondNumberNegative = y.charAt(0) == '-' ? 1 : 0;

// strip the negative symbols from the numbers which are negative
if (isFirstNumberNegative > 0) {
x = x.substring(1);
}
if (isSecondNumberNegative > 0) {
y = y.substring(1);
}

String s1 = new StringBuffer(x).reverse().toString();
String s2 = new StringBuffer(y).reverse().toString();

int[] m = new int[s1.length() + s2.length()];

for (int i=0; i<s1.length(); i++) {
for (int j=0; j<s2.length(); j++) {
m[i+j] = m[i+j] + (s1.charAt(i) - '0') * (s2.charAt(j) - '0');
}
}

String product = new String();

for (int i=0; i<m.length; i++) {
int digit = m[i] % 10;
int carry = m[i] / 10;
if (i+1 < m.length) {
m[i+1] = m[i+1] + carry;
}
product = digit + product;
}

while (product.length() > 1 && product.charAt(0) == '0') {
product = product.substring(1);
}

// if only one number is negative put a negative symbol in front
// if both numbers are negative this condition won't hold true because it will be == 2
// if both numbers are positive this condition won't hold true because it wil be == 0
if (isFirstNumberNegative + isSecondNumberNegative == 1) {
product = new StringBuffer(product).insert(0, '-').toString();
}
System.out.println("The multiplication of\n" + x + " \nand\n" + y + " " + "\nis\n" + product);

scan.close();

关于java - 乘以没有大整数的大整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68746324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com