gpt4 book ai didi

java - 逐个元素地添加数组中的数字

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:10 30 4
gpt4 key购买 nike

目标是将存储在数组中的两个数字逐个元素相加。这些数字不一定需要具有相同的长度。我无法解释结转的可能性。

如果数字是 1101,它将表示为:[1,0,1,1]- 最低有效位在位置 0。我在做加法时没有将其转换为整数。

我正在制作一个单独的方法来计算二进制数的总和,但我只想了解如何使用相同的逻辑来解决这个问题。

例如:349+999 或者它们甚至可以是二进制数,例如 1010101+11

有什么建议吗?

    int carry=0;
int first= A.length;
int second=B.length;
int [] sum = new int [(Math.max(first, second))];
if(first > second || first==second)
{
for(int i =0; i <A.length;i++)
{
for(int j =0; j <B.length;j++)
{
sum[i]= (A[i]+B[j]);
}
}
return sum;
}
else
{
for(int i =0; i <B.length;i++)
{
for(int j =0; j <A.length;j++)
{
sum[i]= (A[i]+B[j]);
}
}
return sum;
}

对于二进制加法:

   byte carry=0;
int first= A.length;
int second=B.length;
byte [] sum = new byte [Math.max(first, second)+1];
if(first > second || first==second)
{
for(int i =0; i < A.length && i!= B.length ;i++)
{
sum[i]= (byte) (A[i] + B[i] + carry);
if(sum[i]>1) {
sum[i] = (byte) (sum[i] -1);
carry = 1;
}
else
carry = 0;
}
for(int i = B.length; i < A.length; i++) {
sum[i] = (byte) (A[i] + carry);
if(sum[i]>1) {
sum[i] = (byte) (sum[i] -1);
carry = 1;
}
else
carry = 0;
}
sum[A.length] = carry; //Assigning msb as carry
return sum;
}
else
{
for(int i =0; i < B.length && i!= A.length ;i++) {
sum[i]= (byte) (A[i] + B[i] + carry);
if(sum[i]>1) {
sum[i] = (byte) (sum[i] -1);
carry = 1;
}
else
carry = 0;
}
for(int i = A.length; i < B.length; i++) {
sum[i] = (byte) (B[i] + carry);
if(sum[i]>1) {
sum[i] = (byte) (sum[i] -1);
carry = 1;
}
else
carry = 0;
}
sum[B.length] = carry;//Assigning msb as carry
return sum;
}

最佳答案

没有必要区别对待二进制和十进制。这可以处理任何基数,从二进制到 base36,并且非常大值 -- 远远超出单纯的整数和长整数!

数字需要从最低位开始添加。将最低有效位放在首位使代码更简单,这就是为什么大多数 CPU 都是 Little-Endian。

注意:将代码保存为“digits.java”——digits 是主课。为了便于阅读,我将 Adder 放在首位。

输出:

NOTE: Values are Little-Endian! (right-to-left)
base1: 0(0) + 00(0) = 000(0)
base2: 01(2) + 1(1) = 110(3)
base2: 11(3) + 01(2) = 101(5)
base2: 11(3) + 011(6) = 1001(9)
base16: 0A(160) + 16(97) = 101(257)
base32: 0R(864) + 15(161) = 101(1025)

源代码:digits.java:

class Adder {
private int base;
private int[] a;
private int[] b;
private int[] sum;

public String add() {
int digitCt= a.length;
if(b.length>digitCt)
digitCt= b.length; //max(a,b)
digitCt+= 1; //Account for possible carry
sum= new int[digitCt]; //Allocate space
int digit= 0; //Start with no carry
//Add each digit...
for(int nDigit=0;nDigit<digitCt;nDigit++) {
//digit already contains the carry value...
if(nDigit<a.length)
digit+= a[nDigit];
if(nDigit<b.length)
digit+= b[nDigit];
sum[nDigit]= digit % base;//Write LSB of sum
digit= digit/base; //digit becomes carry
}
return(arrayToText(sum));
}

public Adder(int _base) {
if(_base<1) {
base= 1;
} else if(_base>36) {
base=36;
} else {
base= _base;
}
a= new int[0];
b= new int[0];
}

public void loadA(String textA) {
a= textToArray(textA);
}

public void loadB(String textB) {
b= textToArray(textB);
}

private int charToDigit(int digit) {
if(digit>='0' && digit<='9') {
digit= digit-'0';
} else if(digit>='A' && digit<='Z') {
digit= (digit-'A')+10;
} else if(digit>='a' && digit<='z') {
digit= (digit-'a')+10;
} else {
digit= 0;
}
if(digit>=base)
digit= 0;
return(digit);
}

private char digitToChar(int digit) {
if(digit<10) {
digit= '0'+digit;
} else {
digit= 'A'+(digit-10);
}
return((char)digit);
}

private int[] textToArray(String text) {
int digitCt= text.length();
int[] digits= new int[digitCt];
for(int nDigit=0;nDigit<digitCt;nDigit++) {
digits[nDigit]= charToDigit(text.charAt(nDigit));
}
return(digits);
}

private String arrayToText(int[] a) {
int digitCt= a.length;
StringBuilder text= new StringBuilder();
for(int nDigit=0;nDigit<digitCt;nDigit++) {
text.append(digitToChar(a[nDigit]));
}
return(text.toString());
}

public long textToInt(String a) {
long value= 0;
long power= 1;
for(int nDigit=0;nDigit<a.length();nDigit++) {
int digit= charToDigit(a.charAt(nDigit));
value+= digit*power;
power= power*base;
}
return(value);
}
}

public class digits {

public static void main(String args[]) {
System.out.println("NOTE: Values are Little-Endian! (right-to-left)");
System.out.println(test(1,"0","00"));
System.out.println(test(2,"01","1"));
System.out.println(test(2,"11","01"));
System.out.println(test(2,"11","011"));
System.out.println(test(16,"0A","16"));
System.out.println(test(32,"0R","15"));
}

public static String test(int base, String textA, String textB) {
Adder adder= new Adder(base);
adder.loadA(textA);
adder.loadB(textB);
String sum= adder.add();
String result= String.format(
"base%d: %s(%d) + %s(%d) = %s(%d)",
base,
textA,adder.textToInt(textA),
textB,adder.textToInt(textB),
sum,adder.textToInt(sum)
);
return(result);
}

}

关于java - 逐个元素地添加数组中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32280764/

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