# 富途2021面试

# 1.实现一个函数,把数组中的0移到最后

function moveZero(arr) {
  const n = arr.length;
  let left = 0, right = 0;
  while(left < n-1){
    if (arr[left] == 0) {
      right = left + 1;
      while (right < n && arr[right] == 0) {
        right++;
      }
      if (right < n) [arr[left], arr[right]] = [arr[right], arr[left]];
    }
    
    left++;
  }
  return arr;
}

console.log(moveZero([0,1,0,3,12]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 实现梯度计费

function calcCost(num) {
  const steps = [5, 20, 50];
  const costs = [30, 15, 10];
  let index = 0;
  let ans = 0;
  let prev = 0;
  while (num - prev > 0) {
    ans += (num - steps[index]) > 0 ? steps[index] * costs[index] : (num - prev) * costs[index];
    prev =  steps[index];
    index++;
  }
  return ans;
} 

console.log(calcCost(16))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15