可以說我有陣列列表A = {9,5,3,7}和陣列列表B = {4,9,8,7,5}。 (這兩個的ArrayLists的長度可以是相同或不同。) 這些的ArrayList表示數字(但寫在至少顯著一階)如何在2個數組列表中添加整數? JAVA
我想創建一個ArrayList(結果),其中包含每個數字的總和與他們在另一個數據列表中的副本。如果總和大於9,那麼剩餘部分將轉移到下一個數字。以上將與下面的步驟相同:7359 + 57894 = 65253
結果必須是一個arraylist:result = {3,5,2,5,6}。
這是我曾嘗試:
BigInt result = new BigInt();
int temp=0;
int carry=0;
BigInt bigger = this;
BigInt smaller = otherBigInt;
if(this.lessOrEqual(otherBigInt)){
smaller = this;
bigger = otherBigInt;
}
for(int i=0; i<bigger.digitList.size(); i++){
temp= bigger.digitList.get(i)+smaller.digitList.get(i)+carry;
carry = temp/10;
result.digitList.add(i,temp%=10);
}
if(carry == 1){
result.digitList.add(1);
}
return result;
我不知道什麼是錯我的代碼..請幫助
A = {9,5,3,7},B = {4,9,8,7,5} - > {3,5,2,5,6} < - 這裏您的邏輯是什麼? – nafas
請提供您現在得到的輸出結果和您期望的結果 – novy1234
提供正確的輸入和期望輸出 – Rajesh