这里写目录标题
L1-1 寻找250 (10 分)
输入的同时处理出结果即可
AC代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
int num, cnt = 0, res = 0;;
while(cin >> num){
if(res != 0) continue;
cnt++;
if(num == 250) res = cnt;
}
cout << res << endl;
return 0;
}
L1-2 日期格式化 (5 分)
送分题~
AC代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
int a, b, c;
scanf("%d-%d-%d", &a, &b, &c);
printf("%d-%02d-%02d\n", c, a, b);
return 0;
}
L1-3 阅览室 (20 分)
思路
注意 00:00 这个借书时间。
用 h * 60 + m 来处理时间,用数组来标记书本的状态即可。
AC代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int s[1005];
int main(int argc, char** argv) {
int t;
cin >> t;
while(t--){
char op, c;
int num, h, m, sum = 0, cnt = 0;
memset(s, -1, sizeof(s));
while(1){
cin >> num >> op >> h >> c >> m;
if(num == 0) break;
if(op == 'S') s[num] = h * 60 + m;
else{
if(s[num] == -1) continue;
cnt++;
sum += h * 60 + m - s[num];
s[num] = -1;
}
}
if(cnt == 0) printf("0 0\n");
else{
double tmp = (1.0 * sum) / (1.0 * cnt);
printf("%d %.0lf\n", cnt, tmp);
}
}
return 0;
}
L1-4 稳赢 (15 分)
用循环来记录轮数即可。
AC代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int s[1005];
map<string, string> res;
int main(int argc, char** argv) {
res["ChuiZi"] = "Bu";
res["JianDao"] = "ChuiZi";
res["Bu"] = "JianDao";
int n;
cin >> n;
string s;
for(int i = 0; ; i++){
cin >> s;
if(s == "End") break;
if(i == n){
cout << s << endl;
i = -1;
}
else cout << res[s] << endl;
}
return 0;
}
L1-5 宇宙无敌大招呼 (5 分)
送分题
AC代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
string s;
cin >> s;
cout << "Hello " << s << endl;
return 0;
}
L1-6 整除光棍 (20 分)
思路
数论题,解释一下样例如何解决:
| 位数 | 过程计算式 | 商 | 余数 |
|---|---|---|---|
| 1 | 1 / 31 | 0 | 1 |
| 2 | 11 / 31 | 0 | 11 |
| 3 | 111 / 31 | 3 | 18 |
| 4 | 181 / 31 | 5 | 26 |
| 5 | 261 / 31 | 8 | 13 |
| 6 | 131 / 31 | 4 | 7 |
| 7 | 71 / 31 | 2 | 9 |
| 8 | 91 / 31 | 2 | 29 |
| 9 | 291 / 31 | 9 | 12 |
| 10 | 121 / 31 | 3 | 28 |
| 11 | 281 / 31 | 9 | 2 |
| 12 | 21 / 31 | 0 | 21 |
| 13 | 211 / 31 | 6 | 25 |
| 14 | 251 / 31 | 8 | 3 |
| 15 | 31 / 31 | 1 | 0 |
都这样了,应该能观察出来要如何模拟吧
AC代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define ll long long
#define chushi(a, b) memset(a, b, sizeof(a))
#define endl "\n"
const double eps = 1e-8;
const ll INF=9223372036854775807;
const int mod = 1e9+7;
const int maxn = 1e6 + 5;
using namespace std;
int main(){
ll x;
cin >> x;
ll a = 1, f = 0, len = 0;
while(1){
ll tmp = a / x;
ll num = a % x;
if(tmp != 0) cout << tmp, f = 1;
else if(f) cout << tmp;
len++;
if(num == 0) break;
a = num * 10 + 1;
}
cout << " " << len << endl;
return 0;
}
L1-7 装睡 (10 分)
按题意判断即可
AC代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
int n;
cin >> n;
string s; int a, b;
while(n--){
cin >> s >> a >> b;
if(a < 15 || a > 20 || b < 50 || b > 70) cout << s << endl;
}
return 0;
}
L1-8 矩阵A乘以B (15 分)
模拟即可,不了解矩阵乘法的点这里转到百度百科~~~
AC代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int x[105][105];
int y[105][105];
int res[105][105] = {0};
int main(int argc, char** argv) {
int a, b;
cin >> a >> b;
for(int i = 1; i <= a; i++){
for(int j = 1; j <= b; j++){
cin >> x[i][j];
}
}
int c, d;
cin >> c >> d;
for(int i = 1; i <= c; i++){
for(int j = 1; j <= d; j++){
cin >> y[i][j];
}
}
if(b != c) cout << "Error: " << b << " != " << c << endl;
else{
for(int i = 1; i <= a; i++){
for(int j = 1; j <= d; j++){
for(int k = 1; k <= d; k++){
res[i][j] += x[i][k] * y[k][j];
}
}
}
cout << a << " " << d << endl;
for(int i = 1; i <= a; i++){
for(int j = 1; j <= d; j++){
cout << res[i][j];
if(j == d) cout << endl;
else cout << " ";
}
}
}
return 0;
}
L2-1 点赞狂魔 (25 分)
思路
用 set 去重即可,按要求排序,实现实现并列部分。
感谢队友给了我一个错误的题意,让我处理了一个多小时的字符串。
AC代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct Node{
string name;
int zongshu;
int zonglei;
int id;
}node;
vector<node> res;
bool cmp(node A, node B){
if(A.zonglei == B.zonglei) return A.zongshu < B.zongshu;
return A.zonglei > B.zonglei;
}
int main(int argc, char** argv) {
int n;
cin >> n;
string name; int k, num; set<int> se;
for(int i = 1; i <= n; i++){
cin >> name >> k;
for(int i = 1; i <= k; i++){
cin >> num;
se.insert(num);
}
int time = se.size(); se.clear();
res.push_back({name, k, time, i});
}
if(n == 1) cout << res[0].name << " - -" << endl;
else if(n == 2) cout << res[0].name << " " << res[1].name << " -" << endl;
else{
sort(res.begin(), res.end(), cmp);
cout << res[0].name << " " << res[1].name << " " << res[2].name << endl;
}
return 0;
}
L2-2 重排链表 (25 分)
思路
双指针处理,一个指向头,一个指向尾,往中间靠即可。
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define ll long long
#define chushi(a, b) memset(a, b, sizeof(a))
#define endl "\n"
const double eps = 1e-8;
const ll INF=9223372036854775807;
const int mod = 1e9+7;
const int maxn = 1e6 + 5;
using namespace std;
int nxt[maxn],lst[maxn], v[maxn];
int A[maxn], B[maxn];
int main(){
int s, n;
cin >> s >> n;
int a, b, data;
for(int i = 1; i <= n; i++){
cin >> a >> data >> b;
nxt[a] = b;
if(b != -1) lst[b] = a;
v[a] = data;
}
int e = s;
while(nxt[e] != -1) e = nxt[e];
while(1){
printf("%05d %d %05d\n", e, v[e], s);
e = lst[e];
if(s == e){
printf("%05d %d -1\n", s, v[s]);
break;
}
printf("%05d %d %05d\n", s, v[s], e);
s = nxt[s];
if(s == e){
printf("%05d %d -1\n", s, v[s]);
break;
}
}
return 0;
}
L2-3 图着色问题 (25 分)
思路
直接暴搜即可,找每个相邻的点看颜色是否相同。
AC代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define ll long long
#define chushi(a, b) memset(a, b, sizeof(a))
#define endl "\n"
const double eps = 1e-8;
const ll INF=9223372036854775807;
const int mod = 1e9+7;
const int maxn = 1e6 + 5;
using namespace std;
vector<int> ma[505];
int v[505], a[505];
set<int> se;
int f = 1;
void DFS(int x, int fa){
if(f == 0 || v[x]) return;
int len = ma[x].size(); v[x] = 1;
for(int i = 0; i < len; i++){
int tmp = ma[x][i];
if(a[tmp] == a[x]){
f = 0;
return;
}
if(v[tmp] == 0) DFS(tmp, x);
}
}
int main(){
int n, m, k;
cin >> n >> m >> k;
int x, y;
for(int i = 1; i <= m; i++){
cin >> x >> y;
ma[x].push_back(y);
ma[y].push_back(x);
}
int N;
cin >> N;
while(N--){
se.clear();
for(int i = 1; i <= n; i++) cin >> a[i], se.insert(a[i]);
if(se.size() != k){
cout << "No" << endl;
continue;
}
f = 1;
for(int i = 1; i <= n; i++) v[i] = 0;
for(int i = 1; i <= n; i++) if(v[i] == 0) DFS(i, 0);
if(f) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
L2-4 部落 (25 分)
思路
并查集裸题
AC代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define ll long long
#define chushi(a, b) memset(a, b, sizeof(a))
#define endl "\n"
const double eps = 1e-8;
const ll INF=9223372036854775807;
const int mod = 1e9+7;
const int maxn = 1e6 + 5;
using namespace std;
int f[maxn];
int v[maxn];
int find(int x){
if(f[x] == x) return x;
else return f[x] = find(f[x]);
}
void join(int x, int y){
int fx = find(x);
int fy = find(y);
if(fx != fy) f[fx] = fy;
}
int main(){
for(int i = 0; i < maxn; i++) f[i] = i;
for(int i = 0; i < maxn; i++) v[i] = 0;
int n;
cin >> n;
for(int i = 1; i <= n; i++){
int k, x, y;
cin >> k >> x; v[x] = 1;
for(int j = 2; j <= k; j++){
cin >> y;
join(x, y);
v[y] = 1;
}
}
int sum = 0, res = 0;
for(int i = 1; i < maxn; i++){
if(v[i]){
sum++;
if(find(i) == i) res++;
}
}
cout << sum << " " << res << endl;
int m, x, y;
cin >> m;
while(m--){
cin >> x >> y;
if(find(x) == find(y)) cout << "Y" << endl;
else cout << "N" << endl;
}
return 0;
}

9365

被折叠的 条评论
为什么被折叠?



