題目大意:給定一個峰巢結構,讓我們模擬小蜜蜂產密。n只蜜蜂產26種蜜並指定在某列產蜜,一旦在某列產蜜,蜜就從頂部跑到底部。如果那列滿了,就永遠也裝不進去了。如果上下列蜜的種類相同則產生一種candy,問最後可產多少candy?
解題思路:簡單模擬題,先把蜂巢結構打表打出來,然後每接受一個輸入,就用棧來模擬,看是要進棧還是退棧,最後輸出有蜜的蜂巢。
測試數據:14
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
代碼:
[csharp]
#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]);
} www.2cto.com
}