好吧,所以我被分配了一个使用堆栈的练习。这个任务的挑战是创建一个程序,可以在不使用任何库或导入的情况下添加和减去非常大的整数(可能是无限大小)(例如,没有BigInteger)。
这就是我接近加法的方式:
public Stack<Integer> sum(Stack<Integer> leadingStack, Stack<Integer> secondStack) {
int carry = 0;
Stack<Integer> resultStack = new Stack<Integer>();
while (leadingStack.isEmpty() == false && secondStack.isEmpty() == false) {
int result = 0;
int dig1 = leadingStack.pop();
int dig2 = secondStack.pop();
int resultDig = 0;
result = dig1 + dig2 + carry;
resultDig = result % 10;
carry = result / 10;
resultStack.push(resultDig);
}
if (carry > 0)
resultStack.push(carry);
return resultStack;
}
此方法似乎适用于某些整数而非其他整数。例如,如果我输入500&amp; 450,我按预期得到950。但是,如果我输入500和45,我会得到45。
这就是我接近减法的方式(非常类似的方法):
public Stack<Integer> sub(Stack<Integer> leadingStack, Stack<Integer> secondStack) {
boolean borrow = false;
Stack<Integer> resultStack = new Stack<Integer>();
while (leadingStack.isEmpty() == false && secondStack.isEmpty() == false) {
int dig1 = leadingStack.pop();
int dig2 = secondStack.pop();
if (borrow = true) {
dig1 -= 1;
borrow = false;
}
if (dig1 - dig2 < 0) {
dig1 += 10;
resultStack.push(dig1 - dig2);
borrow = true;
}
}
return resultStack;
}
这有一个非常相似的问题。例如,如果我减去50和45,我得到4.如果我减去50,000和45,000我得到4,900。
我确信我在这里遗漏了一些简单的东西,但我一遍又一遍地查看代码,我不确定它是什么。
答案 0 :(得分:0)
我开始看看这个情况:
$attachments = get_posts( $args );
if ($attachments) {
$attachmentIds = array();
foreach ( $attachments as $attachment ) {
$attachmentIds[] = $attachment->ID;
}
}
$data['meta_input']['advert_post_attachments'] = $attachmentIds;
这意味着&#34;而两个堆栈都不是空的&#34;。但是如果你有2个不同位数的数字(让我们说500和45),它就会失败(因为一个堆栈将为空,另一个堆栈将赢得&#39; t,在处理所有数字之前离开循环)。因此,您应该将条件更改为&#34;而至少有一个堆栈不为空&#34;:
while (leadingStack.isEmpty() == false && secondStack.isEmpty() == false)
另一个提示:当while (leadingStack.isEmpty() == false || secondStack.isEmpty() == false)
返回布尔值时,您不需要使用isEmpty()
进行比较,您可以这样做:
==
请注意,由于一个堆栈可能为空而另一个堆栈不是,因此在调用while (! leadingStack.isEmpty() || ! secondStack.isEmpty())
之前应检查它是否为空:
pop
答案 1 :(得分:0)
您的代码中很少有地方需要关注:
if (borrow = true)
所有代码:
public Stack<Integer> sum(Stack<Integer> leadingStack, Stack<Integer> secondStack) {
int carry = 0;
Stack<Integer> resultStack = new Stack<Integer>();
while (leadingStack.isEmpty() == false && secondStack.isEmpty() == false) {
int dig1 = leadingStack.pop();
int dig2 = secondStack.pop();
int result = dig1 + dig2 + carry;
int resultDig = result % 10;
carry = result / 10;
resultStack.push(resultDig);
}
Stack<Integer> leftStack = leadingStack.isEmpty() ? secondStack : leadingStack;
while (leftStack.isEmpty() == false) {
int dig = leftStack.pop();
if (carry > 0) {
dig += carry;
carry = 0;
}
resultStack.push(dig);
}
if (carry > 0) resultStack.push(carry);
return reverse(resultStack);
}
public Stack<Integer> sub(Stack<Integer> leadingStack, Stack<Integer> secondStack) {
boolean borrow = false;
Stack<Integer> resultStack = new Stack<Integer>();
if (leadingStack.size() < secondStack.size()) {
// Handle negative number
}
while (leadingStack.isEmpty() == false && secondStack.isEmpty() == false) {
int dig1 = leadingStack.pop();
int dig2 = secondStack.pop();
if (borrow) {
dig1 -= 1;
borrow = false;
}
if (dig1 < dig2) {
dig1 += 10;
resultStack.push(dig1 - dig2);
borrow = true;
}
else {
resultStack.push(dig1 - dig2);
}
}
Stack<Integer> leftStack = leadingStack.isEmpty() ? secondStack : leadingStack;
while (leftStack.isEmpty() == false) {
int dig = leftStack.pop();
if (borrow) {
dig -= 1;
borrow = false;
}
resultStack.push(dig);
}
if (borrow) {
// Handle negative number
}
return reverse(resultStack);
}
private Stack<Integer> reverse(Stack<Integer> inStack) {
Stack<Integer> outStack = new Stack<>();
while (inStack.isEmpty() == false) outStack.push(inStack.pop());
return outStack;
}