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;
}
相关推荐
qq_459234425 天前
【题库】| 商用密码应用安全性评估从业人员考核题库(四十)
职场和发展·密码学·学习方法·考核·商用密码·商用密码应用安全性评估·密评
敲敲了个代码5 天前
[特殊字符] 空数组的迷惑行为:为什么 every 为真,some 为假?
前端·javascript·react.js·面试·职场和发展
诚思报告YH5 天前
视频面试软件市场洞察:2026 - 2032年复合年均增长率(CAGR)为10.3%
面试·职场和发展
重生之后端学习5 天前
74. 搜索二维矩阵
开发语言·数据结构·算法·职场和发展·深度优先
tyb3333335 天前
leetcode:吃苹果和队列
算法·leetcode·职场和发展
Pitiless-invader5 天前
MySQL 相关知识及面试问题汇总
面试·职场和发展
重生之后端学习5 天前
35. 搜索插入位置
java·数据结构·算法·leetcode·职场和发展·深度优先
逆境不可逃5 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
筱昕~呀5 天前
冲刺蓝桥杯-DFS板块(第二天)
算法·蓝桥杯·深度优先
zheshiyangyang5 天前
前端面试基础知识整理【Day-10】
前端·面试·职场和发展