題目:一個數組,讓數組的每個元素去除第一個元素,得到的商作爲被除數所在位置的新值。
代碼實現:
public class JavaInterview_1 {
public static void main(String[] args) {
int [] arr = new int[]{5,3,6,9,12,4};
for(int i = arr.length - 1; i >= 0; i--){
arr[i] /= arr[0];
}
for(int e : arr){
System.out.print(e + " ");
}
}
}
運行結果:
PS:這裏新手容易形成定式思維,容易想到從前往後迭代進行運算(得不到想要的結果),不太會反向思考。