目标是将两个数字相加,这些数字一元一元地存储在数组中。数字不一定需要具有相同的长度。我无法解释结转的可能性。
如果数字是1101,它将被表示为:[1,0,1,1] - 最低有效位在位置0.我正在进行加法而不将其转换为整数。
我正在制作一个单独的方法来计算二进制数的总和,但我只是想了解如何使用相同的逻辑来解决这个问题。
Ex: 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;
}
答案 0 :(得分:2)
不需要以不同方式处理二进制和十进制。 这可以处理任何基础,从二进制到base36,以及极其 大的价值 - 远远超出了int和long的长度!
首先需要从最不重要的数字中添加数字。 首先放置最低有效数字会生成代码 更简单,这就是大多数CPU都是Little-Endian的原因。
注意:将代码保存为“digits.java” - 数字为 主要班级。为了便于阅读,我首先添加了Adder。
输出:
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);
}
}
源代码:digits.java:
{{1}}
答案 1 :(得分:0)
首先,添加二进制数将不同于整数。对于int,你可以做类似
的事情int first = A.length;
int second = B.length;
int firstSum = 0;
for (int i = 0; i < first; i++){
firstSum += A[i] * (10 ^ i);
}
int secondSum = 0;
for (int j = 0; j < second; j++){
secondSum += B[j] * (10 ^ j);
}
int totalSum = firstSum + secondSum;
答案 2 :(得分:0)
它应该在下面表示为这样,因为我们只需要在两个数组的相同位置添加值。此外,在您的第一个if
条件中,它应该是||
而不是&&
。这个算法应该完美的工作。如果有任何并发症,请告诉我。
int carry=0;
int first= A.length;
int second=B.length;
int [] sum = new int [Math.max(first, second)+1];
if(first > second || first==second)
{
for(int i =0; i < A.length && i!= B.length ;i++)
{
sum[i]= A[i] + B[i] + carry;
if(sum[i]>9) {
sum[i] = sum[i] -9;
carry = 1;
}
else
carry = 0;
}
for(int i = B.length; i < A.length; i++) {
sum[i] = A[i] + carry;
if(sum[i]>9) {
sum[i] = sum[i] -9;
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]= A[i] + B[i] + carry;
if(sum[i]>9) {
sum[i] = sum[i] -9;
carry = 1;
}
else
carry = 0;
}
for(int i = A.length; i < B.length; i++) {
sum[i] = B[i] + carry;
if(sum[i]>9) {
sum[i] = sum[i] -9;
carry = 1;
}
else
carry = 0;
}
sum[B.length] = carry //Assigning msb as carry
return sum;
}