for(int i = 0; i < n; i ++) { int num; cin >> num; a.insert(num); }
int res = 0; for(auto i = a.begin(); i != a.end(); i ++) for(auto j = next(i); j != a.end(); j ++) { int k = *i, m = *j; if(k < m && gcd(k, m) == 1) res ++; }
最终的结果使用set存储,不用再考虑去重问题。输出次大值使用auto it = next(area.begin()),其中it是一个迭代器(iterator),使用 set 的 begin()、end()、next() 等方法获取迭代器时,这些迭代器是指向 set 中元素的指针。 这意味着 it 变量存储的是 指向 集合中某个元素的指针,而不是元素本身的值 。所以输出指针的值使用解引用*,表明取 it 指向的内存地址上的值
// // Created by HUAWEI on 2025/3/6. // #include<iostream> #include<algorithm> #include<cstring> #include<set>
usingnamespace std;
constint N = 110; int g[N][N]; bool st[N][N]; set<int, greater<int>> area; int n, m; int t; // 每个黑区域的大小
int dx[4] = {0, 0, -1, 1}; int dy[4] = {1, -1, 0, 0};
voiddfs(int a, int b) { if(st[a][b] || g[a][b] == 0) return;
t ++; st[a][b] = true; for(int i = 0; i < 4; i ++) { int x = a + dx[i], y = b + dy[i]; if(x >= 1 && x << n && y >= 1 && y <= m && !st[x][y] && g[a][b] == 1) dfs(x, y); }