admin管理员组

文章数量:1029802

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

打怪(模拟)

  • 打怪
代码语言:javascript代码运行次数:0运行复制
#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;
}

字符串分类(字符串哈希)

  • 字符串分类
代码语言:javascript代码运行次数: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)

  • 城市群数量
代码语言:javascript代码运行次数:0运行复制
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);
            }
        }
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-04-19,如有侵权请联系 cloudcommunity@tencent 删除returnset字符串dfsint

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

打怪(模拟)

  • 打怪
代码语言:javascript代码运行次数:0运行复制
#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;
}

字符串分类(字符串哈希)

  • 字符串分类
代码语言:javascript代码运行次数: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)

  • 城市群数量
代码语言:javascript代码运行次数:0运行复制
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);
            }
        }
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-04-19,如有侵权请联系 cloudcommunity@tencent 删除returnset字符串dfsint

本文标签: 今日三题打怪(模拟)字符串分类(字符串哈希)城市群数量(dfs)