美团 8.13 笔试

1、魔法外卖
一系列截止时间和每次送快递需要的时间,有超能力可以把快递送达不需要耗费时间,问你至少需要使用几次超能力。

void magicTakeout() {
  int res = 0;
  int n,t;
  cin >> n >> t;
  vector<int> time(n,0);
  for (int i=0; i<n; i++) {
    cin >> time[i];
  }
  sort(time.begin(), time.end());
  int nowtime = t;
  for (int i=0; i<time.size(); i++) {
    if (time[i]<nowtime) {
      res++;
    } else {
      nowtime += t;
    }
  }
  cout << res << endl;

}

2、扫地机器人
一个扫地机器人,可以上下左右走在n*m空间内,字符串s表示当前预设移动顺序,例如"WWASSD",求能否全部扫到。

void robotMove(vector<vector<bool>> &visitor, int &sum, int &x, int &y, char cmd) {
  if (cmd == 'W') {
    y--;
  } else if (cmd == 'A') {
    x--;
  } else if (cmd == 'S') {
    y++;
  } else if (cmd == 'D') {
    x++;
  }
  if (!visitor[x][y]) {
    visitor[x][y] = true;
    sum++;
  }
}

void SweepRobot() {
  int n,m,k;
  cin >> n >> m >> k;
  string cmd;
  cin >> cmd;
  vector<vector<bool>> visitor(n, vector<bool>(m, false));
  int sum = 1, nowx=0, nowy=0;
  visitor[0][0] = true;
  for (int i=0; i<cmd.size(); i++) {
    robotMove(visitor, sum, nowx, nowy, cmd[i]);
    if (sum == n*m) {
      cout << "Yes" << endl;
      cout << i+1 <<endl;
      return;
    }
  }
  cout << "No" << endl;
  cout << n*m-sum << endl;
}

3、扑克模拟

void pokerGame() {
  int n;
  cin >> n;
  vector<int> array(n,0);
  for (int i=0; i<n; i++) {
    cin >> array[i];
  }
  deque<int> poker;
  for (int i=n-1; i>=0; i--) {
    poker.push_front(array[i]);
    poker.push_front(poker[poker.size()-1]);
    poker.pop_back();
    poker.push_front(poker[poker.size()-1]);
    poker.pop_back();
  }
  for (int i=0; i<n; i++) {
    cout << poker[i];
    if (i!=n-1) {
      cout << "";
    }
  }
  cout << endl;
}

4、合法元组数
求三元组,满足i<j<k && nums[i] - 2nums[j] =2*nums[j] - nums[k]

void legalTuple() {
  int n;
  cin >> n;
  vector<int> array(n, 0);
  for (int i=0; i<n; i++) {
    cin >> array[i];
  }
  map<int, vector<int>> mp;
  for (int i=0; i<n; i++) {
    int tmp = array[i]*3;
    if (mp.find(tmp) == mp.end()) {
      mp[tmp] = {i};
    } else {
      mp[tmp].push_back(i);
    }
  }
  int res = 0;
  for (int i=0; i<n; i++) {
    for (int j=i+2; j<n; j++) {
      int tmp = array[i] + array[j];
      if (mp.find(tmp) != mp.end()) {
        for (auto &x: mp[tmp]) {
          if (x>i && x<j) {
            res++;
          }
        }
      }
    }
  }
  cout << res << endl;
}

5、二叉树最长根路径和

void vistorTree( vector<int>& money, int& res, int nowsum, int index) {
  int n = money.size();
  if (index >= n) {
    if (nowsum > res) {
      res = nowsum;
    }
    return;
  }
  nowsum += money[index];
  vistorTree(money, res, nowsum, index*2+1);
  vistorTree(money, res, nowsum, index*2+2);
}

void getTreeSum() {
  int n;
  cin >> n;
  vector<int> money(n,0);
  for (int i=0; i<n; i++) {
    cin >> money[i];
  }
  int res = 0;
  vistorTree(money, res, 0, 0);
  cout << res << endl;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值