- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试比较两个手动创建且未使用内置 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;
的程度它不会返回或退出循环。这意味着在下一次迭代中该值将被覆盖。这意味着突然13
和23
被认为是平等的。
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/
我正在使用 gmp 执行复杂的操作。我想使用 Botan 来执行加密功能。问题是他们都有自己的 Bigint 函数。因此,在将 gmp 函数中使用的 bigint 值提供给 Botan 函数时会产生问
我正在尝试运行查询: let query = ` DELETE FROM ${table_name} WHERE
我有以下代码片段: use num_bigint::*; // 0.2.2 use num_traits::*; // 0.2.8 use std::ops::*; fn xgcd(b: &BigIn
我有一个 Postgres 8.2 表,其中列定义为 bigint 数据类型。 当我执行 sum(column) 时,返回类型是 numeric。我想强制将总和类型转换为 bigint,因为我确信结果
我有这样一个场景: CREATE TABLE `Users` ( `IdUser` bigint(20) NOT NULL PRIMARY KEY ) ENGINE=InnoDB DEFAULT
我想遍历一系列具有 BigUint 类型的值(来自 num crate )。 我该怎么做? 我试过了 for i in 0..a {...} 其中 a 是(借用的)BigUint 类型。我收到有关不匹
环境: Ubuntu 14.04 MySql 工作台 6.2.4 MariaDB 10 当我尝试将模型与数据库模式同步时,我已经在一个表中定义了 UNSIGNED BIGINT 类型(即 UNSIGN
我正在使用一个列来存储 UNIX 时间戳以秒为单位(除以 1000)。我发现 bigint 数据类型足以存储它。我使用 创建了它 ... createTimeStamp bigint, ...
我假设一种语言的实现允许您将指针视为整数,包括对它们进行标准算术。如果由于硬件限制这是不现实的,请告诉我。如果编程语言通常没有这么强大的指针运算,但是在实践中是可行的,那么我仍然想知道这种实现BigI
我正在尝试使用 Spark 将数据从 greenplum 移动到 HDFS。我可以从源表中成功读取数据,数据框(greenplum 表)的 spark 推断模式是: 数据框架构: je_header
我有一个表名称file_upload它的 upload_id 列为 BIGINT(11) AI PK NOT NULL当我将表列大小更改为 BIGINT(20) 时,AI 标志被删除,每次更改列中的任
我必须将一些加密代码从我不太熟悉的 java (visual c++) 移植到 visual c++。我在 http://sourceforge.net/projects/cpp-bigint/ 找到
我正在尝试对任意大整数实现 Solovoy-Strassen 素性检验。我还将编写一个 bignum(不能使用第 3 方实现,因为这是一个学术项目)。我已经决定了 bignum 的以下结构: stru
我有两个模型(商店和产品),两者的主键都是 BigInt,但产品(store_id)中的关系列仍然是整数。我不想使用原始 SQL,如何使用 Django 解决此问题? 例子: class Produc
有没有办法得到 BigInt 的对数?在 JavaScript 中? 对于普通数字,您将使用以下代码: const largeNumber = 1000; const result = Math.lo
在我的嵌入式项目中,我有一个处理任意长度整数的 biginteger 类。我希望能够生成一个介于 0 和任意数字之间的随机 bigint。假设我有一个高质量的随机字节源。 我见过的所有实现基本上都做同
我有一个 pandas pyspark 中的数据框.我想将此数据框创建/加载到 hive table 。 pd_df = pandas data frame id
有没有办法获得可以存储在 bigint 中的最大值,而无需对其进行硬编码? 是否有返回/包含此值的函数或常量? 最佳答案 请参阅 this similar question 中提供的答案.据我所知,没
我正在使用 Chapel,我正在尝试对 bigint 执行计算多语言环境设置中的数组。读取每行包含一个整数的文件。每行转换为 bigint记录然后插入到单个数组中。我有 4 个语言环境,因此我要求每个
我有一个存储过程,它必须返回一个 bigint 作为输出。下面如果定义。 在正文中,我在表中插入一行并在 @LogID 输出变量中使用 @@Identity 返回标识。 除返回部分外,一切正常。我试过
我是一名优秀的程序员,十分优秀!