【强训笔记】day23

NO.1

思路:直接计算结果,先计算怪物可以抗几次攻击,再计算勇士受到的伤害,如果勇士的攻击力大于等于怪物的血量,那么就可以击杀无数只,如果勇士的血量正好是受到攻击的整数倍,那么击杀的怪物数量就减一。

代码实现:

cpp 复制代码
#include<iostream>

using namespace std;

int t;
int h,a,H,A;

int fun()
{
    if(a>=H) return -1;
    
    int m=(H/a)+(H%a!=0?1:0);
    int n=m-1;
    int x=n*A;
    int ret=h/x-(h%x==0?1:0);
    return ret;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>h>>a>>H>>A;
        cout<<fun()<<endl;
    }
    return 0;
}

NO.2

思路:哈希表,利用set的哈希表可以去重。

代码实现:

cpp 复制代码
#include <iostream>
#include<string>
#include<algorithm>
#include<unordered_set>
using namespace std;

int n;
string s;

int main() {
   cin >> n;
 unordered_set<string> hash;
 while(n--)
 {
 cin >> s;
 sort(s.begin(), s.end());
 hash.insert(s);
 }
 cout<<hash.size()<<endl;
     }

NO.3

思路:dfs,定义一个bool数组用来标记,如果该位置为false,那么就ret++,再dfs搜索,dfs中将搜索过的位置的bool数组标记为true,再对该位置进行dfs搜索,搜索完后直接返回ret就可以了。

代码实现:

cpp 复制代码
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(m[pos][i]&&!vis[i])
            {
                dfs(m,i);
            }
        }
    }
};
相关推荐
wdfk_prog14 分钟前
嵌入式面试真题学习笔记系列
笔记·学习·面试
懿路向前17 分钟前
【HarmonyOS学习笔记】2026-07-30 | 小艺开放平台智能体接入实战
笔记·学习·harmonyos
冻柠檬飞冰走茶1 小时前
PTA基础编程题目集 7-27 冒泡法排序(C语言实现)
c语言·开发语言·数据结构·算法
wdfk_prog1 小时前
canopen学习笔记系列
笔记·学习
大鱼>2 小时前
因子挖掘+回测+RL交易:量化金融完整系统
人工智能·深度学习·算法·金融
砚凝霜4 小时前
软考网络工程师|第 4 章 移动通信、CDMA 码分多址 完整备考笔记
网络·笔记
c238564 小时前
《枚举算法 “翻译官”:用 enum class 给 “游游的礼盒” 算分》
c语言·c++·算法
吃好睡好便好4 小时前
MATLAB中图像的对数变换
图像处理·学习·算法·计算机视觉·matlab
noipp4 小时前
推荐题目:洛谷 P5843 [SCOI2012] Blinker 的噩梦
c语言·数据结构·c++·算法·游戏·洛谷·luogu
极客BIM工作室4 小时前
OpenCascade 倒角(Chamfer)算法完整逻辑解析
算法