- 团队
我们的聊天室-Our chat room(以弃用)
- 2025-7-14 13:35:46 @
Let,s chat!****
30 条评论
-
-
//小明爱集合 #include #include <unordered_set> #include #include
using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
int T; cin >> T; while (T--) { int n, m; cin >> n >> m; unordered_set<int> setA; for (int i = 0; i < n; ++i) { int num; cin >> num; setA.insert(num); } int common = 0; for (int i = 0; i < m; ++i) { int num; cin >> num; if (setA.count(num)) { common++; } } int totalUnique = setA.size() + m - common; int similarity = (totalUnique == 0) ? 0 : static_cast<int>((common * 100.0) / totalUnique); cout << similarity << "\n"; } return 0;
} //约瑟夫问题 #include #include
using namespace std;
int main() { int n, m; cin >> n >> m;
vector<int> people(n); for (int i = 0; i < n; ++i) { people[i] = i + 1; // 编号从1到n } vector<int> result; int pos = 0; // 当前报数的起始位置 while (!people.empty()) { pos = (pos + m - 1) % people.size(); // 计算出列位置 result.push_back(people[pos]); people.erase(people.begin() + pos); // 移除出列的人 } for (int num : result) { cout << num << " "; } cout << endl; return 0;
} //八个方向 #include #include #include
using namespace std;
// 八个方向:上、下、左、右、左上、右上、左下、右下 const int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; const int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
bool canFindTreasure(int N, const vector<vector>& maze) { vector<vector> visited(N, vector(N, false)); queue<pair<int, int>> q;
// 起点是 (0, 0)(因为题目描述是 (1,1),但代码中通常从 0 开始) if (maze[0][0] == 1) return false; // 题目保证起点没有蝙蝠,但可以加上检查 if (maze[0][0] == 2) return true; // 起点就是宝藏 q.push({0, 0}); visited[0][0] = true; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); for (int i = 0; i < 8; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) { if (maze[nx][ny] == 2) return true; if (maze[nx][ny] == 0) { visited[nx][ny] = true; q.push({nx, ny}); } } } } return false;
}
int main() { int N; cin >> N;
vector<vector<int>> maze(N, vector<int>(N)); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cin >> maze[i][j]; } } if (canFindTreasure(N, maze)) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0;
} //马走日 #include #include
using namespace std;
// 马的8种移动方向(“日”字形) const int dx[] = {1, 2, 2, 1, -1, -2, -2, -1}; const int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
int count = 0;
void dfs(int x, int y, int n, int m, vector<vector>& visited, int steps) { if (steps == n * m) { count++; return; }
for (int i = 0; i < 8; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny]) { visited[nx][ny] = true; dfs(nx, ny, n, m, visited, steps + 1); visited[nx][ny] = false; // 回溯 } }
}
int main() { int T; cin >> T;
while (T--) { int n, m, x, y; cin >> n >> m >> x >> y; vector<vector<bool>> visited(n, vector<bool>(m, false)); visited[x][y] = true; // 标记起点 count = 0; dfs(x, y, n, m, visited, 1); // 初始步数为1(起点已访问) cout << count << endl; } return 0;
} //全排列问题 #include #include
using namespace std;
void generatePermutations(int n, vector& current, vector& used) { if (current.size() == n) { for (int num : current) { cout << num << " "; } cout << endl; return; }
for (int i = 1; i <= n; ++i) { if (!used[i]) { used[i] = true; current.push_back(i); generatePermutations(n, current, used); current.pop_back(); used[i] = false; } }
}
int main() { int n; cin >> n;
vector<int> current; vector<bool> used(n + 1, false); // 数字从1到n generatePermutations(n, current, used); return 0;
}
-
#include <bits/stdc++.h> using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(0);
int N; long long C; cin >> N >> C; vector<long long> a(N); unordered_map<long long, long long> countMap; for (int i = 0; i < N; i++) { cin >> a[i]; countMap[a[i]]++; } long long ans = 0; if (C == 0) { for (int i = 0; i < N; i++) { ans += (countMap[a[i]] - 1); } } else { for (int i = 0; i < N; i++) { long long y = a[i] - C; if (y < 0) { continue; } if (countMap.find(y) != countMap.end()) { ans += countMap[y]; } } } cout << ans << endl; return 0;
}
-
#include<bits/stdc++.h> using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(0);
int t; cin >> t; while (t--) { int m, n; cin >> m >> n; // m: 集合B的大小, n: 集合A的大小 vector<int> B(m); vector<int> A(n); // 读取集合B的元素 for (int i = 0; i < m; i++) { cin >> B[i]; } // 读取集合A的元素 for (int i = 0; i < n; i++) { cin >> A[i]; } // 创建计数数组(元素范围1~50000) vector<int> count(50001, 0); // 统计集合B中每个元素的出现次数 for (int num : B) { if (num >= 1 && num <= 50000) { count[num]++; } } bool isSubset = true; // 检查集合A的元素是否都存在于集合B中且数量不超过 for (int num : A) { if (num < 1 || num > 50000) { isSubset = false; break; } if (count[num] > 0) { count[num]--; } else { isSubset = false; break; } } if (isSubset) { cout << "Yes\n"; } else { cout << "No\n"; } } return 0;
}
-
#include using namespace std; struct node{ char data; int left,right; }; node Betree[10]; void pre(int root){ if(root>=0){ cout<<Betree[root].data; pre(Betree[root].left); pre(Betree[root].right); } } void in(int root){ if(root>=0){ in(Betree[root].left); cout<<Betree[root].data; in(Betree[root].right); } } void back(int root){ if(root>=0){ back(Betree[root].left); back(Betree[root].right); cout<<Betree[root].data; } } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ cin>>Betree[i].data>>Betree[i].left>>Betree[i].right; } pre(0); cout<<endl; in(0); cout<<endl; back(0)l; cout<<endl; return 0; }
-
同志们 人呢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
#include iostream #include queue using namespace std; int main() { queue<int1️⃣ q; int num; for (int i = 0; i < 5; i++) { cin >> num; q.push(num); } for (int i = 0; i < 3; i++) { int front = q.front(); q.pop(); if (i > 0) { cout << " "; } cout << front; } cout << endl; cout << q.front() << endl; return 0; } 1️⃣:>
-
#include<bits/stdc++.h> using namespace std; struct node{ int data; node *next; node *prev; }; int main() { node a={1,NULL,NULL}; node b={2,NULL,NULL}; node c={3,NULL,NULL}; a.next=&b; b.next=&c; c.prev=&b; b.prev=&a; node d={4,NULL,NULL};//头部 node e={5,NULL,NULL};//b,c之间 node f={6,NULL,NULL};//尾部 node *pd=&d; node *pa=&a; node *pb=&b; node *pe=&e; node *pc=&c; node *pf=&f; pa->prev=pd; pd->next=pa; pe->next=pc; pe->prev=pb; pb->next=pe; pc->prev=pe; pc->next=pf; pf->prev=pc; node *p1=&d; while(p1!=NULL){ cout<<p1->data<<" "; p1=p1->next; } cout<<endl; node *p2=&f; while(p2!=NULL){ cout<<p2->data<<" "; p2=p2->prev; } return 0; }
-
#include using namespace std; //定义结构体 struct node{ int data; node *next; node *pre; }; int main(){ //1、定义3个结点并连接起来 node a={1, NULL, NULL}; node b={2, NULL, NULL}; node c={3, NULL, NULL}; a.next=&b; b.pre=&a; b.next=&c; c.pre=&b; node *p=&c; while(p!=NULL){ cout<data<<" "; p=p->pre; }
return 0;
}
-
#include using namespace std; struct node{ char data; node *next; }; int main() { node a={'A',NULL}; node b={'B',NULL}; node c={'C',NULL}; node d={'D',NULL}; node e={'E',NULL}; a.next=&b; b.next=&c; c.next=&d; d.next=&e; node *p; p=&a; while(p!=NULL){ cout<<①>adta<<endl; p=p->next; } return 0; } ①:p-
- 1