C++ Map操作求助
查看原帖
C++ Map操作求助
704724
raysun007楼主2023/2/5 21:10

C++ Map操作求助

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<set>
#include<unordered_set>
#include<vector>
#include<climits>
#include<limits>
#include<map>
#include<unordered_map>

using namespace std;

int main() {
  int s1, s2, s3;
  cin >> s1 >> s2 >> s3;
  
  unordered_map<int, int> map;
  for (int i = 1; i <= s1; i++) {
    for (int j = 1; j <= s2; j++) {
      for (int k = 1; k <= s3; k++) {
        int v = i + j + k;
        // 方法1: 注释处写法AC
        // if (map.count(v)) {
        //   int t = map[v];
        //   map.erase(v);
        //   map.insert(make_pair(v, t + 1));
        // } else {
        //   map.insert(make_pair(v, 1));
        // }

        // 方法2: 此处写法40分
        int t = map[v];
        map[v] = t + 1;
      }
    }
  }

  int max = 0;
  int v = 1000000;
  for (auto iter = map.begin(); iter != map.end(); iter++) {
    if (iter->second >= max) {
      v = std::min(v, iter->first);
      max = iter->second;
    }
  }  
  cout << v << endl;
  return 0;
}

个人认为对map执行覆盖更新的2处写法等价,请问为什么不能AC?或者不同之处时什么?

求助讨论版同学帮忙解答,谢谢

2023/2/5 21:10
加载中...