题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4230
题目大意:给定一个峰巢结构,让我们模拟小蜜蜂产密。n只蜜蜂产26种蜜并指定在某列产蜜,一旦在某列产蜜,蜜就从顶部跑到底部。如果那列满了,就永远也装不进去了。如果上下列蜜的种类相同则产生一种candy,问最后可产多少candy?
EA
EB
EC
ED
EE
EF
EG
EH
EI
EJ
EK
EK
EG
EL
11
EA
EA
EA
EA
EA
EA
EA
EA
EA
EA
EA
11
DA
DA
DA
DA
DA
DA
DA
DA
DA
DA
DA
11
IA
IA
IA
IA
IA
IA
IA
IA
IA
IA
IA
题目大意:给定一个峰巢结构,让我们模拟小蜜蜂产密。n只蜜蜂产26种蜜并指定在某列产蜜,一旦在某列产蜜,蜜就从顶部跑到底部。如果那列满了,就永远也装不进去了。如果上下列蜜的种类相同则产生一种candy,问最后可产多少candy?
解题思路:简单模拟题,先把蜂巢结构打表打出来,然后每接受一个输入,就用栈来模拟,看是要进栈还是退栈,最后输出有蜜的蜂巢。
EA
EB
EC
ED
EE
EF
EG
EH
EI
EJ
EK
EK
EG
EL
11
EA
EA
EA
EA
EA
EA
EA
EA
EA
EA
EA
11
DA
DA
DA
DA
DA
DA
DA
DA
DA
DA
DA
11
IA
IA
IA
IA
IA
IA
IA
IA
IA
IA
IA
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int st[40][40];
int top[40],n,m,ans;
char candy[11000];
char hive[40][40];
char ori[40][40] = {
" ",
" _",
" _/ \\_",
" _/ \\_/ \\_",
" _/ \\_/ \\_/ \\_",
" _/ \\_/ \\_/ \\_/ \\_",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
" \\_/ \\_/ \\_/ \\_/",
" \\_/ \\_/ \\_/",
" \\_/ \\_/",
" \\_/",
};
int main()
{
int i,j,k,tpcol,tp,colsum;
while (scanf("%d",&n) != EOF) {
ans = 0;
memcpy(hive,ori,sizeof(ori));
memset(top,0,sizeof(top));
for (i = 0; i < n; ++i) {
scanf("%s",candy);
tpcol = (candy[0] - 'A' + 1) * 2 - 1;
colsum = 11 - abs(9 - tpcol) / 2;
tp = candy[1] - 'A';
//用栈操作模拟计算糖果数
if (top[tpcol] == 0) {
top[tpcol]++;
st[tpcol][top[tpcol]] = tp;
}
else if (top[tpcol] < colsum) {
if (st[tpcol][top[tpcol]] == tp)
top[tpcol]--,ans++;
else st[tpcol][++top[tpcol]] = tp;
}
}
printf("The number of candy is %d.\n",ans);
//改变峰巢内容
for (i = 1; i <= 17; i += 2) {//column
int tpmin = 2 + abs(i-9)/2;
int tpmax = 22 - abs(i-9)/2;
for (k = 1,j = tpmax; k <= top[i]; j -= 2,++k)
hive[j][i] = st[i][k] + 'A';// printf("%d %d\n",j,i),
}
for (i = 1; i <= 23; ++i)
printf("%s\n",hive[i]);
}
}
本文ZeroClock原创,但可以转载,因为我们是兄弟。