次の仮定で行く:
- 500 は [5,0,0] として表されます。
- 被減数は
mi
- 減数は
su
次のアルゴリズムを使用して、整数の 2 つの配列を減算できます。
- と比較
mi.length
するsu.length
- if
mi.length
< su.length
then をスワップしarray
、2 つの配列間で減算した後 (最後の index から0th
index に移動する列ごとの減算を行う)、結果を無効にします。
mi.length
==の場合su.length
、mi ( mi[i]
) のインデックス i の要素と su ( su[i]
) のインデックス i の要素の比較を開始します。mi
より小さいことが判明した場合はsr
、 を交換しarray
ます。2 つの配列間の減算の後、結果を否定します。
- if
mi.length > su.length
then は、2 つの配列間で通常の減算を実行するだけです。
足し算と引き算の完全なコードを次に示します。
//Assumed that none of number represented by array is LPaded by "0s" and the initegers at other than 0th position is not negative.
public class BigNumberAddSubtract
{
private int array1[];
private int array2[];
private boolean shouldBeNegative = false;
public BigNumberAddSubtract(int[] iArray1, int[] iArray2)
{
array1 = iArray1;
array2 = iArray2;
}
public void setArray1(int[] array)
{
this.array1 = array;
}
public void setArray2(int[] array)
{
this.array2 = array;
}
//Subtraction
private int[] subtract()
{
int length1 = array1.length;
int length2 = array2.length;
boolean isMinus = false;
int mi[] = array1 ;//Minuend
int su[] = array2 ;//Subtrahend
boolean isPrevEqual = false;
boolean isFirstNegative = false;
if (length1 == length2 )
{
if (array1[0] < 0 )
{
isFirstNegative = true;
}
for (int i =0 ; i < length1 ; i++)
{
if (Math.abs(array1[i]) == Math.abs(array2[i]))
{
isPrevEqual = true;
continue;
}
else if (array1[i] < array2[i])
{
if (isPrevEqual)
{
isMinus = true;
System.out.println("breaking");
break;
}
}
else
{
isPrevEqual = false;
}
}
}
else if (length1 < length2)
{
if (array1[0] < 0)
{
isFirstNegative = true;
}
isMinus = true;
}
else
{
if (array1[0] < 0 )
{
isFirstNegative = true;
}
}
array1[0] = Math.abs(array1[0]);
array2[0] = Math.abs(array2[0]);
if (isMinus)
{
mi = array2;
su = array1;
}
int[] result = new int[mi.length];
int iCursor1 = mi.length - 1;
int iCursor2 = su.length - 1;
int iCarry = 0;
for (int i=result.length - 1 ; i >= 0 ; i--, iCursor1--, iCursor2--)
{
int iValue = 0;
int iFrom = 0;
iFrom = mi[iCursor1] - iCarry;
if (iFrom < (iCursor2 < 0 ? 0 : su[iCursor2]))
{
if (iCursor1 > 0)
{
iFrom = 10 + iFrom;
}
iCarry = 1;
}
else
{
iCarry = 0;
}
iValue = iFrom - (iCursor2 < 0 ? 0 : su[iCursor2]) ;
result[i] = iValue;
}
int[] temp = null;
if (isMinus)
{
int counter = 0;
for (int i=0; i < result.length; i++)
{
if (result[i] == 0)
{
counter++ ;
continue;
}
else
{
result[i] = -result[i];
break;
}
}
temp = new int[result.length - counter];
System.arraycopy(result,counter,temp,0,result.length - counter);
result = new int[temp.length];
System.arraycopy(temp,0,result,0,temp.length);
}
if (isFirstNegative)
{
int counter = 0;
for (int i=0; i < result.length; i++)
{
if (result[i] == 0)
{
counter++ ;
continue;
}
else
{
result[i] = -result[i];
break;
}
}
temp = new int[result.length - counter];
System.arraycopy(result,counter,temp,0,result.length - counter);
result = new int[temp.length];
System.arraycopy(temp,0,result,0,temp.length);
}
return result;
}
//Addition
private int[] add()
{
int length1 = array1.length;
int length2 = array2.length;
int[] result = new int[length1 > length2 ? length1 : length2];
int iCursor1 = array1.length - 1;
int iCursor2 = array2.length - 1;
int iCarry = 0;
array1[0] = Math.abs(array1[0]);
array2[0] = Math.abs(array2[0]);
for (int i=result.length - 1 ; i >= 0 ; i--, iCursor1--, iCursor2--)
{
int iValue = 0;
if (iCursor1 >= 0 && iCursor2 >= 0)
{
iValue = array1[iCursor1] + array2[iCursor2] + iCarry;
}
else if (iCursor1 < 0 && iCursor2 >=0)
{
iValue = array2[iCursor2] + iCarry;
}
else if (iCursor1 >=0 && iCursor2 < 0)
{
iValue = array1[iCursor1] + iCarry;
}
if (iCursor1 == 0 && iCursor2 ==0)
{
result[i] = iValue;
iCarry = 0;
}
else
{
result[i] = iValue % 10;
iCarry = iValue / 10;
}
}
result[0] = result[0] + iCarry;
if (shouldBeNegative)
{
result[0] = -result[0];
}
return result;
}
public int[] performOperation()
{
if (array1[0] < 0 && array2[0] < 0)//If digit at one's place of both number is negative
{
shouldBeNegative = true;
return add();
}
else if (array1[0] > 0 && array2[0] > 0)
{
return add();
}
else //if digit at one's place of one of the numbers is negative
{
return subtract();
}
}
public static void main(String[] st)
{
int[] arr1 ={-9,0,0,6};
int[] arr2 = {1,9,7,8};
BigNumberAddSubtract big = new BigNumberAddSubtract(arr1,arr2);
int[] addResult = big.performOperation();
for (int i : addResult )
{
System.out.print(i);
}
}
}
テストが失敗したり、矛盾が見つかった場合は、お知らせください。