【今日三题】打怪(模拟) / 字符串分类(字符串哈希) / 城市群数量(dfs)

目录
- 打怪(模拟)
- 字符串分类(字符串哈希)
- 城市群数量(dfs)
打怪(模拟)
- 打怪
#include <iostream>
using namespace std;int h, a, H, A;int func()
{if (a >= H) return -1;int n = H / a + (H % a == 0 ? 0 : 1); // 怪物能抗几次int m = n - 1; // 玩家被攻击几次int k = m * A; // 杀死一只怪物玩家掉多少血int res = h / k - (h % k == 0 ? 1 : 0);return res;
}int main()
{int t;cin >> t;while (t--){cin >> h >> a >> H >> A;cout << func() << endl;}return 0;
}
字符串分类(字符串哈希)
- 字符串分类
#include <iostream>
#include <string>
#include <unordered_set>
#include <algorithm>
using namespace std;int n;
unordered_set<string> set;int main()
{cin >> n;while (n--){string s;cin >> s;sort(s.begin(), s.end());set.insert(s);}cout << set.size() << endl;return 0;
}
城市群数量(dfs)
- 城市群数量
class Solution {
public:int used[201] = {};int citys(vector<vector<int> >& m) {int res = 0;for (int i = 0; i < m.size(); i++){if (!used[i]){res++;dfs(m, i);}}return res;}void dfs(vector<vector<int>>& m, int pos){used[pos] = 1;for (int i = 0; i < m.size(); i++){if (!used[i] && m[pos][i]){dfs(m, i);}}}
};
本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~
