当前位置: 首页 > news >正文

Week8

7-1 100...07是不是素数?

每个数暴力 \(O(\sqrt{v})\) 找最小质因子即可,时间复杂度 \(O(n\cdot \sqrt{v})\)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;int min_p(ll x){if(x % 2 == 0) return 2;for(int i=3; 1ll*i*i<=x; i+=2){if(x % i == 0) return i;}return x;
}void solve(){int n;cin >> n;ll cur = 10;for(int k=0; k<=n; k++){int p = min_p(cur + 7);if(p == cur+7){cout << cur+7 << " is prime!\n";}else{cout << cur+7 << " factor=" << p << '\n';}cur *= 10;}
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1; // cin >> t;while(t--) solve();return 0;
}

7-2 President's Office

先找 c,找到 c 之后去找周围的字母,找到就 dfs 把整个区域都标记为 true,ans++。

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;vector<pii> moves = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void solve(){int n, m; char c;cin >> n >> m >> c;vector<vector<char>> mat(n + 2, vector<char>(m + 2, '.'));for(int i=1; i<=n; i++){string s; cin >> s;for(int j=1; j<=m; j++){mat[i][j] = s[j-1];}}int ans = 0;vector<vector<bool>> vis(n + 2, vector<bool>(m + 2));auto dfs = [&](auto&& self, int x, int y) ->void {vis[x][y] = true;for(int i=0; i<4; i++){int dx = moves[i].first, dy = moves[i].second;int nx = dx + x, ny = dy + y;if(!vis[nx][ny] && mat[nx][ny] == mat[x][y]){self(self, nx, ny);}}};for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){if(mat[i][j] != c) continue;for(int k=0; k<4; k++){int dx = moves[k].first;int dy = moves[k].second;int x = dx + i, y = dy + j;if(!vis[x][y] && mat[x][y] != c && mat[x][y] != '.'){dfs(dfs, x, y);ans++;}}}}cout << ans << '\n';
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1; //cin >> t;while(t--) solve();return 0;
}

7-3 L-shapes

找到 * 就 dfs,维护连通块中每个 * 的坐标,然后根据坐标判断形状是否合法,一共有 4 种合法情况,之后判断有没有别的连通块相邻

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;vector<pii> moves = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
void solve(){int n, m;cin >> n >> m;vector<vector<char>> mat(n + 2, vector<char>(m + 2, '.'));for(int i=1; i<=n; i++){string s; cin >> s;for(int j=1; j<=m; j++){mat[i][j] = s[j-1];}}vector<vector<bool>> vis(n + 2, vector<bool>(m + 2));// dfs 时 , 用 set 收集连通块的点set<pii> st;auto dfs = [&](auto&& self, int x, int y) ->void {vis[x][y] = true;st.insert({x, y});for(int i=0; i<4; i++){int dx = moves[i].first, dy = moves[i].second;int nx = dx + x, ny = dy + y;if(!vis[nx][ny] && mat[nx][ny] == '*'){self(self, nx, ny);}}};bool ok = true;for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){if(!vis[i][j] && mat[i][j] == '*'){dfs(dfs, i, j);if(st.size() >= 4) ok = false; // 只能是 3 个方块// 这里取出的是 x 最小的那个点int x = (*st.begin()).first;int y = (*st.begin()).second;// 若不是这 4 种情况, 则为 falseif((st.count({x+1, y}) && st.count({x+1, y+1})) || (st.count({x+1, y}) && st.count({x+1, y-1})) ||(st.count({x, y+1}) && st.count({x+1, y+1})) ||(st.count({x+1, y}) && st.count({x, y+1}))){}else ok = false;// 检查是否和别的 L 相邻for(auto it : st){int cx = it.first, cy = it.second;for(int k=0; k<8; k++){int dx = moves[k].first, dy = moves[k].second;int nx = dx + cx, ny = dy + cy;if(mat[nx][ny] == '*' && !st.count({nx, ny})) ok = false;}}st.clear();}}}cout << (ok? "YES":"NO") << '\n';
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1; cin >> t;while(t--) solve();return 0;
}

7-4 h0324. 走方格

每一步要么往右走,要么往下走,直接 dfs 暴搜即可

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;void solve(){int n, m;cin >> n >> m;ll ans = 0;auto dfs = [&](auto&&self, int x, int y) ->void {if(x>n || y>m) return;if(x == n && y == m){ans++;return; }self(self, x+1, y);self(self, x, y+1);};dfs(dfs, 0, 0);cout << ans << '\n';}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1; //cin >> t;while(t--) solve();return 0;
}

7-5 h0193. 排列

( 样例是每个数字后都有空格,题目竟然没说 )

进行 n 轮,每次都从没选的点中选择一个点,用 track 记录路径,回溯

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;constexpr int inf = 0x3f3f3f3f;void solve(){int n;cin >> n;vector<bool> used(n + 1);vector<int> track;auto dfs = [&](auto&& self, int x) ->void {if(x == n){for(int i=0; i<n; i++){cout << track[i] << ' ';}cout << '\n';return;}for(int i=1; i<=n; i++){if(used[i]) continue;used[i] = true;track.push_back(i);self(self, x+1);used[i] = false;track.pop_back();}};dfs(dfs, 0);}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1; //cin >> t;while(t--) solve();return 0;
}

推荐直接 next_permutation

void solve(){int n;cin >> n;vector<int> a(n + 1);iota(a.begin(), a.end(), 0); // 递增赋值 , a = {0, 1, 2, ...}do{for(int i=1; i<=n; i++){cout << a[i] << " ";}cout << '\n';}while(next_permutation(a.begin()+1, a.end()));
}

7-6 h0115. 算24

把 4 个数放进一个数组里,每次取出两个数进行 + - * / 操作,将结果放入。重复操作,仅剩一个数时即为结果。因为设计 double 运算,判相等时最好使用 abs(A-B) <= eps 而不是直接 A == B

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;constexpr double eps = 1e-6;bool ed = false;
void solve(){vector<double> a(4);for(int i = 0; i < 4; i++) cin >> a[i];if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0){ed = true;return;}bool ok = false;auto dfs = [&](auto&& self, vector<double>& v) -> void {if(ok) return;int n = v.size();if(n == 1){if(fabs(v[0] - 24.0) < eps) ok = true;return;}for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){if(i == j) continue;vector<double> nxt;for(int k = 0; k < n; k++){if(k != i && k != j) nxt.push_back(v[k]);}double x = v[i], y = v[j];// +nxt.push_back(x + y);self(self, nxt);nxt.pop_back();// -nxt.push_back(x - y);self(self, nxt);nxt.pop_back();// *nxt.push_back(x * y);self(self, nxt);nxt.pop_back();// /if(fabs(y) > eps){nxt.push_back(x / y);self(self, nxt);nxt.pop_back();}}}};dfs(dfs, a);cout << (ok ? "YES" : "NO") << '\n';
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);while(!ed){solve();}return 0;
}

7-7 生命游戏II

根据题意模拟即可

#include <bits/stdc++.h>
using namespace std;int k, n;
vector<vector<int>> cur, nxt;
vector<vector<bool>> vis;int dx[8] = {-1,-1,-1,0,0,1,1,1};
int dy[8] = {-1,0,1,-1,1,-1,0,1};int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> k >> n;cur.assign(n+2, vector<int>(n+2, 0));nxt.assign(n+2, vector<int>(n+2, 0));for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {cin >> cur[i][j];}}while(k--) {for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {nxt[i][j] = 0; // 默认边界为0}}for(int i = 2; i < n; i++) {for(int j = 2; j < n; j++) {int live = 0;for(int dir = 0; dir < 8; dir++) {int ni = i + dx[dir], nj = j + dy[dir];live += cur[ni][nj];}if(cur[i][j] == 1) {nxt[i][j] = (live == 2 || live == 3);} else {nxt[i][j] = (live == 3);}}}swap(cur, nxt);}// DFS 统计八连通块大小int cnt = 0;auto dfs = [&](auto&&self, int x, int y) ->void {vis[x][y] = true;cnt++;for(int dir = 0; dir < 8; dir++) {int nx = x + dx[dir], ny = y + dy[dir];if(nx >= 1 && nx <= n && ny >= 1 && ny <= n && !vis[nx][ny] && cur[nx][ny] == 1) {self(self, nx, ny);}}};// DFS 统计最大连通块vis.assign(n+2, vector<bool>(n+2, false));int ans = 0;for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {if(cur[i][j] == 1 && !vis[i][j]) {cnt = 0;dfs(dfs, i, j);ans = max(ans, cnt);}}}cout << ans << "\n";return 0;
}

7-8 L-朋友圈

题意:给你一个无向图,找连通块的个数和最大的连通块大小

使用邻接表存边,gra[u] 是一个数组 {v0, v1, v2...},表示 u 和 v0, v1, v2... 有边

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;void solve(){int n, m;cin >> n >> m;vector<vector<int>> gra(n);for(int i = 0; i < m; i++){int u, v;cin >> u >> v;gra[u].push_back(v);gra[v].push_back(u);}vector<int> vis(n, 0);int cnt = 0;        // 朋友圈个数int mx = 0;         // 最大朋友圈人数int sz = 0;auto dfs = [&](auto&& self, int u) ->void {vis[u] = 1;sz++;for(int v : gra[u]){ // 遍历 u 的每条边if(!vis[v]){self(self, v);}}};for(int i = 0; i < n; i++){if(!vis[i]){sz = 0;cnt++;dfs(dfs, i);mx = max(mx, sz);}}cout << cnt << ' ' << mx << '\n';
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);solve();return 0;
}

7-9 回溯法的通用伪代码(以分书问题为例)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;void solve(){int n, m;cin >> n >> m;   // m = 4vector<vector<int>> like(n + 1, vector<int>(m + 1));vector<int> used(m + 1), ans(n + 1);bool found = false;for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){cin >> like[i][j];}}auto dfs = [&](auto&& self, int x) -> void {if(found) return;if(x > n){found = true;return;}for(int j = 1; j <= m; j++){ // 枚举第 i 个人拿了哪本书if(!like[x][j]) continue;if(used[j]) continue;used[j] = 1;ans[x] = j;self(self, x + 1);used[j] = 0;if(found) return;}};dfs(dfs, 1);if(found){cout << '(';for(int i = 1; i <= n; i++){if(i > 1) cout << ", ";cout << ans[i];}cout << ')';}
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);solve();return 0;
}

7-10 被n整除的n位数

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;void solve(){int n;ll a, b;cin >> n >> a >> b;vector<ll> ans;auto dfs = [&](auto&& self, int pos, ll val) -> void {if(pos > n){ans.push_back(val);return;}int lo = (pos == 1 ? 1 : 0);for(int d = lo; d <= 9; d++){ll nv = val * 10 + d;if(nv % pos == 0){self(self, pos + 1, nv);}}};dfs(dfs, 1, 0);sort(ans.begin(), ans.end());bool ok = false;for(ll x : ans){if(x >= a && x <= b){cout << x << '\n';ok = true;}}if(!ok) cout << "No Solution" << '\n';
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1;while(t--) solve();return 0;
}
http://www.gsyq.cn/news/105385.html

相关文章:

  • leetcode 754. Reach a Number 到达终点数字-耗时100%
  • 豆包手机助手技术预览版发布,AI直接嵌入操作系统底层有何意义?会对行业产生什么影响?
  • 【Agent】MemOS 源码笔记---(5)---记忆分类
  • JSON 与 MongoDB:直存对象的便利与隐性代价
  • 【原创代码改进】基于IVY(常青藤优化算法)-BiTCN(双向时域卷积网络)-BiGRU(双向门控循环单元)的多变量时间序列回归
  • Java毕设选题推荐:基于SpringBoot+Vue智能公寓管理系统基于springboot公寓管理系统【附源码、mysql、文档、调试+代码讲解+全bao等】
  • PSD-95抗体:如何为缺血性脑卒中治疗开启神经保护新纪元?
  • 【课程设计/毕业设计】基于Java的高校澡堂洗浴管理系统基于springboot高校洗浴管理系统【附源码、数据库、万字文档】
  • 9、Python 命名规范与代码优化实践
  • 六自由度机械臂步进电机驱动仿真的MATLAB逆解及Simscape仿真
  • 10kV线路微机继电保护装置源码+配套PCB图纸及BOM表,缩短开发周期学习素材
  • 2026 人工智能YOLOV相关毕业论文选题方向及题目示例(深度学习/yolov/自然语言处理/图像处理/机器学习)​
  • 【开题答辩全过程】以 基于Java高考志愿填报推荐系统为例,包含答辩的问题和答案
  • 【Linux网络编程】TCP Socket
  • 迅达CADI调试软件3.11.3/3.10:5系GX与7系TX操作说明
  • AI伦理治理:在创新与规范之间寻找动态平衡
  • 10、编写和发布 Python 包的实用指南
  • 新零售第一阶段传统零售商的困境突破与二次增长路径——基于定制开发AI智能名片S2B2C商城小程序的实践研究
  • 警惕Vibe Coding ,Agentic Coding认知升级与实践避坑指南
  • 基于博途1200plc的堆垛立体车库设计:IO分配表、电气接线图、PLC程序、组态界面程序与动画仿真
  • Hutool工具库实战:8大核心工具类深度解析
  • 敏捷第15讲:需求变更控制——迭代做了一半老板突然要加“春节红包”,接还是不接?
  • 构建高效性能自动化监控体系的五大核心策略
  • iOS 组件化:模块拆分、依赖反转、解耦实践
  • 【Linux网络编程】UDP Socket
  • 零基础转行AI产品经理:大模型学习路线与面试题库全攻略
  • AI从“玩具”到“工具”的鸿沟如何跨越?一文读懂智能体工程Agent Engineering!
  • SATT-CNN-BiLSTM:基于层结构自注意力机制的卷积连接Bi-LSTM时序预测模型
  • 自动化测试的未来:超越脚本编写
  • 告别“消失的小目标”:航拍图像检测新框架,精度飙升25.7%的秘诀