這個練習是一個簡單具有挑戰性的Java程序。輸入包含:使用給定的整數數組達到目標總和的算法,以避免某些數字?
- 大小的陣列的 「n」 的,
- 用於陣列A中的輸入和
- 爲大小的另一個數組B中的輸入 「N-1」
- 「finalsum」是陣列A中所有元素的總和
什麼是最正確的算法,用於打印添加數組A中所有元素以達到最終和的正確順序,即「finalsum」,從而避免求和達到排列B中的任何值。
Inputs: (split to three lines for clarity)
1.
3 //n, the size of the array
2 4 6 //array a of size n
4 8 //array b of size n-1
//finalsum = 2 + 4 + 6 = 12. Similarly for the 2nd input
Output: 0 1 2 (or) 2 1 0 is also correct
but 1 0 2 is wrong because it cannot add up to 4, since 4 is present in the array b
2.
20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
209 208 207 206 205 204 203 202 201 200 199 198 197 196 195 194 193 192 191
Output: 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 (or) many other ways too
這是一個家庭作業問題嗎? – mellamokb
這是一個裝箱問題。看看它。 –
我試圖解決這個問題,它適用於某些輸入,但是它會給其他輸入提供ArrayIndexOutOfBoundsException錯誤,我不理解清除該錯誤所需的條件。 –