[笔试训练](二十三)067:打怪068:字符串分类069:城市群数量

目录

067:打怪

068:字符串分类

069:城市群数量


067:打怪

题目链接:打怪 (nowcoder.com)

题目:

题解:

直接计算结果:

1.一只怪物能抗几次攻击 int m=(H/a)+(H%a==0?0:1);

2.杀死一只怪物,玩家要抗几次攻击 int n=m-1;

*3.杀死一只怪物,玩家掉多少血 int x=n*A;

4.玩家能杀死多少怪物 int ret=h/x-(h%x==0?1:0); 

#include<iostream>
using namespace std;
int t,h,a,H,A;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>h>>a>>H>>A;
        if(A==0 || a>=H) 
        {
            cout<<-1<<endl;
        }
        else
        {
            int m=(H/a)+(H%a==0?0:1);
            int n=m-1;
            int x=n*A;
            int ret=h/x-(h%x==0?1:0);
            cout<<ret<<endl;
        }
    }
    
    return 0;
}

068:字符串分类

题目链接:字符串分类_牛客笔试题_牛客网 (nowcoder.com)

题目:

题解:

1.将字符串进行排序

2.每个字符串依次存入去重容器 unordered_set 中

3.容器中的元素个数即为字符串的种类数

#include <iostream>
#include<string>
#include<algorithm>
#include<unordered_set>
using namespace std;
int N=0,ret=0;
string s;
unordered_set<string> hashSet;
int main() 
{
    cin>>N;
    while(N--)
    {
        cin>>s;
        sort(s.begin(),s.end());
        hashSet.insert(s);
    }
    cout<<hashSet.size()<<endl;
    return 0;
}

069:城市群数量

题目链接:城市群数量_牛客题霸_牛客网 (nowcoder.com)

题目:

题解:

深度优先搜索:

1.将每个没有标记过的城市 i 进行一次dfs。

2.dfs中遍历每个城市 j ,当在矩阵m中有m[i][j]=1(即有相连关系时),则继续dfs向深搜索。

3.dfs的同时将搜索过的城市用bool数组vis标记下。

class Solution {
public:
    bool vis[210]={0};
    int citys(vector<vector<int> >& m) 
    {
        int n=m.size();
        int ret=0;
        for(int i=0;i<n;i++)
        {
            if(!vis[i])
            {
                ret++;
                dfs(m,i);
            }
        }
        return ret;
    }
    void dfs(vector<vector<int>>& m,int pos)
    {
        vis[pos]=true;
        for(int i=0;i<m.size();i++)
        {
            if(!vis[i] && m[pos][i])
            {
                dfs(m,i);
            }
        }
    }
    
};
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲敲er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值