gpt4 book ai didi

java - 如何与Java中的BigInt进行比较以确定哪个是更大的BigInt

转载 作者:行者123 更新时间:2023-12-01 21:49:10 43 4
gpt4 key购买 nike

我正在尝试比较两个手动创建且未使用内置 BigInt 类的 BigInt。现在我正试图确定如何找到 2 中较大的数字。例如,如果我想找到 123 和 134 之间哪个数字更大,并且我传递了两个我想返回的 BigInts如果 123 是第一个传递的数字,则返回 false;如果传递第二个数字,则返回 True。请看下面的代码:

private boolean thisBigIntGreaterThanOrEqualToOther(BigInt b1, BigInt b2){
boolean value = true;
if(b1.bigInt.size() >= b2.bigInt.size()){
for(int i = 0; i < b2.bigInt.size(); i++){
if(b1.bigInt.get(i) >= b2.bigInt.get(i)){
value = true;
}
else{
value = false;
}
}
}
else{
value = false;
}
return value;
}

正如您在我的代码中看到的,我考虑过尝试比较每个数字,但是当每个数字都为 1 时,我遇到了一个问题,它将值设置为 true。

下面的 BigInt 类:

    public class BigInt {

//Instance variables for the class, a boolean for the sign and an ArrayList to store the input String

private boolean pos = true;
private ArrayList<Integer> bigInt = new ArrayList<Integer>();

//No argument constructor, creates a big integer of value zero

public BigInt () {
this.pos = true;



}

//Constructor for big integers input as int

public BigInt (int newBigInt) {
String inputInt = Integer.toString(newBigInt);
inputInt = handleSign(inputInt);
inputInt = checkNumber(inputInt);
for(int i = inputInt.length() - 1; i >=0; i--) {
bigInt.add(Integer.parseInt(inputInt.substring(i, i+1)));
}
}

//Constructor for big integers input as strings

public BigInt (String newBigInt) {

newBigInt = handleSign(newBigInt);
newBigInt = checkNumber(newBigInt);
for(int i = newBigInt.length() - 1; i >=0; i--) {
bigInt.add(Integer.parseInt(newBigInt.substring(i, i+1)));
}
}
private String handleSign(String num) {

if(num.charAt(0) == '+' || num.charAt(0) == '-') {
if(num.length() == 1) {
throw new ErrorMessage("Invalid value: sign only, no integer.");
}

if(num.charAt(0) == '-') {
this.pos = false;
}

else {
this.pos = true;
}
num = num.substring(1);
}

return num;
}
// Private method to remove leading zeros from add/subtract methods

private BigInt removeZeros(BigInt result){

for(int i = 0; i < result.bigInt.size(); i++){
if(result.bigInt.get(i) == 0){
result.bigInt.remove(i);
}
}
return result;
}

//Private method to check the number; remove leading zeros and check for leading spaces (throw error message)

private String checkNumber(String num) {

if(num.charAt(0) == ' ') {
throw new ErrorMessage("Invalid value: leading blank space.");
}
if(num.charAt(0) == '0'){
while(num.length() > 1 && num.charAt(0) == '0') {
num = num.substring(1);
}
}
return num;
}

//toString method

public String toString() {

String answer = "";
for(int i = bigInt.size() - 1; i >=0; i--) {
answer = answer + bigInt.get(i);
}
if(this.pos == false){
return "-" + answer;
}
return answer;

最佳答案

比较两个的方法BigInt s 以多种方式被破坏:

<强>1。您迭代的方向错误:

for(int i = 0; i < b2.bigInt.size(); i++)

从最低有效数字开始,这意味着 20将被视为小于 11 。将其更改为

for(int i = b2.bigInt.size() - 1; i >= 0 ; i--)

<强>2。您覆盖比较结果

如果您的代码达到了设置 value = false; 的程度它不会返回或退出循环。这意味着在下一次迭代中该值将被覆盖。这意味着突然1323被认为是平等的。

BigInt c = new BigInt("13");
BigInt d = new BigInt("23");
System.out.println(BigInt.thisBigIntGreaterThanOrEqualToOther(c, d));
System.out.println(BigInt.thisBigIntGreaterThanOrEqualToOther(d, c));

输出为

true
true

更改value = false;return false;

<强>3。您不检查是否 b1.bigInt.size() > b2.bigInt.size()

这会导致您的方法返回 131小于 23 .

按以下方式更改代码:

if(b1.bigInt.size() > b2.bigInt.size()){
return true;
} else if(b1.bigInt.size() < b2.bigInt.size()){
return false;
} else {
// the other comparison code
}

一些最后的评论:

实现 Comparable interface 是一个很好的设计因为它允许您在类中使用许多库方法。

编辑:代码现在不再使用库函数

public class BigInt implements Comparable<BigInt> {

...

@Override
public int compareTo(BigInt other) {
int c = this.bigInt.size() - other.bigInt.size();
if (c != 0) {
return c;
} else {
for (int i = this.bigInt.size() - 1; i >= 0; i--) {
c = this.bigInt.get(i) - other.bigInt.get(i);
if (c != 0) {
return c;
}
}
return 0;
}
}
}

关于java - 如何与Java中的BigInt进行比较以确定哪个是更大的BigInt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35448595/

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