12.日期问题 - 蓝桥云课
这个题目主要考察的是日期问题
闰年的判断还
日期的去重和排大小(可以用map实现,但我用的vector+pair)
#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的数组和一个标记是否访问过的标签
#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
#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; }