蓝桥杯历届题目

还有6天蓝桥杯了

开始刷历届练习题了。

小计算器

题意

实现一个直接的计算器。没有四则运算规则优先级。并且能够有进制转换

题解

数字转换字符串的时候,$0$是关键点。

ac代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=(a);i<(n);i++)
#define per(i,a,n) for(int i=(n-1);i>=(a);i--)
#define fi first
#define se second
typedef pair <int,int> pII;
typedef long long ll;
typedef long long ull;
const int INF = 0x3f3f3f3f;
//head
int n;
const int maxchar = 100;
const int maxn = 100;
char s[maxn];
int k;
char ans[maxn];
void solve(ull x){
memset(ans,0,sizeof(ans));
ull t = k;
int cnt = 0;
if(x==0) ans[0] = '0';
while(x){
if(x%t < 10){
ans[cnt++] = x%t+'0';
}else{
ans[cnt++] = x%t+'A'-10;
}
x/=t;
}
int len = strlen(ans);
rep(i,0,len/2) swap(ans[i],ans[len-1-i]);
}
char tt[maxn];
ull real_num(){
int len = strlen(tt);
ull t = 0;
ull cnt = 1;
per(i,0,len){
if(tt[i]>='A' && tt[i]<='Z'){
t += cnt * (tt[i] - 'A' +10);
}else{
t += cnt * (tt[i] - '0');
}
cnt*= k;
}
return t;
}
int main(){
#ifdef LOCAL
freopen("1.in","r",stdin);
#endif
cin>>n;
ull now = 0;
int sign = 0;
k = 10;
bool init = false;
rep(i,0,n){
cin>>s;
if(s[0] == 'C' && s[1] == 'L') {
now = 0;
init = true;
}else if(s[0] == 'N'){
cin>>tt;
ull temp = real_num();
if(init){
now = temp;
init = false;
continue;
}
if(sign == 1) now += temp;
else if(sign == 2) now -= temp;
else if(sign == 3) now *= temp;
else if(sign == 4) now /= temp;
else now %= temp;
}else if(s[0] == 'A'){
sign = 1;
}else if(s[0] == 'S'){
sign = 2;
}else if(s[0] == 'M' && s[1] == 'U'){
sign = 3;
}else if(s[0] == 'D'){
sign = 4;
}else if(s[0] == 'M'){
sign = 5;
}else if(s[0] == 'E'){
solve(now);
cout<<ans<<'\n';
}else{
cin>>k;
}
}
return 0;
}

合根植物

题意

并查集

数组开小了。

ac代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=(a);i<(n);i++)
#define per(i,a,n) for(int i=(n-1);i>=(a);i--)
#define fi first
#define se second
typedef pair <int,int> pII;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//head

const int maxn = 2e3+10;
int fa[maxn*maxn];
int Find(int x){
if(fa[x] == x) return x;
else return fa[x] = Find(fa[x]);
}
int Union(int x,int y){
int i = Find(x);int j = Find(y);
if(i!=j) fa[j] = i;
}
int n,m;
int k;
int main(){
#ifdef LOCAL
freopen("2.in","r",stdin);
#endif
scanf("%d%d%d",&n,&m,&k);
rep(i,1,n*m+1) fa[i] = i;
rep(i,0,k){
int x,y; scanf("%d%d",&x,&y);
Union(x,y);
}
int ans = 0;
rep(i,1,n*m+1) if(fa[i] == i) ans++;
printf("%d\n",ans);
return 0;
}