解题思路:简单模拟题,先把蜂巢结构打表打出来,然后每接受一个输入,就用栈来模拟,看是要进栈还是退栈,最后输出有蜜的蜂巢。
#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原创,但可以转载,因为我们是兄弟。