hh蓝桥杯每日一题

12.日期问题 - 蓝桥云课

这个题目主要考察的是日期问题

闰年的判断还

日期的去重和排大小(可以用map实现,但我用的vector+pair)

cpp 复制代码
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
bool isleapyear(int y)
{
	if(y%400==0)return true;
	if(y%100==0)return false;
	return y%4==0;
}

bool check(int yy,int month,int day,int &fullyear)
{
	if(month<1||month>12)return false;
	if(yy>=40)fullyear=1900+yy;
	else fullyear=2000+yy;
	if(fullyear<1960||fullyear>2059)
	{
		return false;
	}
	if(day<1)return false;
	
	if(month==2)
	{
		if(isleapyear(fullyear))
		{
			return day<=29;
		}
		else return day<=28;
	}
	
	if(month==4||month==6||month==9||month==11)return month<=30;
	return day<=31; 
}

int datetoint(int y,int m,int d)
{
	return y*10000+m*100+d;
}

int main()
{
	string s;
	cin>>s;
	
	int a = (s[0] - '0') * 10 + (s[1] - '0');
    int b = (s[3] - '0') * 10 + (s[4] - '0');
    int c = (s[6] - '0') * 10 + (s[7] - '0');
    
    vector<pair<int,string>> dates;
    
    int year1;
    if(check(a,b,c,year1))
    {
    	int dateint=datetoint(year1,b,c);
    	char buf[20];
    	sprintf(buf, "%04d-%02d-%02d", year1, b, c);
        dates.push_back({dateint, buf});
	}
	
	int year2;
    if(check(c,b,a,year2))
    {
    	int dateint=datetoint(year2,b,a);
    	char buf[20];
    	sprintf(buf, "%04d-%02d-%02d", year2, b, a);
        dates.push_back({dateint, buf});
	}
	
	int year3;
    if(check(c,a,b,year3))
    {
    	int dateint=datetoint(year3,a,b);
    	char buf[20];
    	sprintf(buf, "%04d-%02d-%02d", year3, a, b);
        dates.push_back({dateint, buf});
	}
	
	 sort(dates.begin(), dates.end());
    
    // 去重并输出
    string lastDate = "";
    for (const auto& p : dates) {
        if (p.second != lastDate) {
            cout << p.second << endl;
            lastDate = p.second;
        }
    }
    return 0;
}

14.油漆面积 - 蓝桥云课

求这个的面积可以转化成标记这个图上的位置是否被访问过来求

这样就很简单了,准备一个n*n的数组和一个标记是否访问过的标签

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

const int N = 10010;
bool marked[N][N];  // 标记数组,记录每个点是否被覆盖

int main() {
    int n;
    cin >> n;
    
    // 初始化标记数组
    memset(marked, 0, sizeof(marked));
    
    int total = 0;
    
    for(int i = 0; i < n; i++) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        
        // 确保x1<=x2, y1<=y2
        if(x1 > x2) swap(x1, x2);
        if(y1 > y2) swap(y1, y2);
        
        // 标记这个矩形覆盖的区域
        for(int x = x1; x < x2; x++) {
            for(int y = y1; y < y2; y++) {
                if(!marked[x][y]) {
                    marked[x][y] = true;
                    total++;
                }
            }
        }
    }
    
    cout << total << endl;
    return 0;
}

17.发现环 - 蓝桥云课

这个有两种做法

利用拓扑排序

或者是并查集和dfs

cpp 复制代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

const int N = 100010;
vector<int> g[N];  // 邻接表
int degree[N];     // 每个节点的度
bool inCycle[N];   // 是否在环上

int main() {
    int n;
    cin >> n;
    
    // 初始化
    for(int i = 0; i <= n; i++) {
        g[i].clear();
        degree[i] = 0;
        inCycle[i] = true;  // 初始假设所有节点都在环上
    }
    
    // 读入边
    for(int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
        degree[a]++;
        degree[b]++;
    }
    
    // 拓扑排序:删除所有不在环上的节点(度为1的节点)
    queue<int> q;
    for(int i = 1; i <= n; i++) {
        if(degree[i] == 1) {
            q.push(i);
            inCycle[i] = false;
        }
    }
    
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        
        for(int v : g[u]) {
            degree[v]--;
            if(degree[v] == 1 && inCycle[v]) {
                q.push(v);
                inCycle[v] = false;
            }
        }
    }
    
    // 输出环上的节点
    bool first = true;
    for(int i = 1; i <= n; i++) {
        if(inCycle[i]) {
            if(!first) cout << " ";
            cout << i;
            first = false;
        }
    }
    cout << endl;
    
    return 0;
}
相关推荐
Swift社区17 小时前
LeetCode 469 凸多边形
算法·leetcode·职场和发展
回眸&啤酒鸭18 小时前
【回眸】WLB头马俱乐部第九次参会——跨年英语演讲
职场和发展·演讲能力·职场充电
程序员三藏18 小时前
单元测试详解
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
悄悄敲敲敲18 小时前
MySQL复合查询
面试·职场和发展
CCPC不拿奖不改名18 小时前
面向对象编程:继承与多态+面试习题
开发语言·数据结构·python·学习·面试·职场和发展
橘颂TA18 小时前
【剑斩OFFER】算法的暴力美学——力扣:1047 题:删除字符串中的所有相邻重复项
c++·算法·leetcode·职场和发展·结构于算法
Dontla18 小时前
Mock Interview模拟面试,20251225,MNC第一面HR面,AI Engineer
职场和发展
零售ERP菜鸟21 小时前
IT价值证明:从“成本中心”到“增长引擎”的确定性度量
大数据·人工智能·职场和发展·创业创新·学习方法·业界资讯