|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.超市内商品的信息包括商品编号和生产日期。用于商品展示的货架,是一个半封闭的货架,客人拿取商品,工作人员上架新的商品,都只能固定在开放的一端进行。当工作人员上架新商品时,需要整理展示架上的商品,使生产日期较早的商品,放在靠近固定开放的一端。请编程模拟对商品展示架中已有商品按生产日期进行整理的过程。
2.“小猫钓鱼”的游戏规则是:将一副扑克牌平均分成两份,每人拿一份。玩家甲先拿出手中的第一张扑克牌放在桌上,然后玩家乙也拿出手中的第一张扑克牌,并放在玩家甲刚打出的扑克牌的上面,就像这样两个玩家交替出牌。出牌时,如果某人打出的牌与桌上某张牌的牌面相同,即可将两张相同的牌及其中间所夹的牌全部取走,并依次放到自己手中牌的末尾。当任意一个人手中的牌全部出完时,游戏结束,对手获胜。
要求编写程序来模拟这场游戏,并判断出谁最后获胜,获胜的同时打印出获胜者手中的牌以及桌上可能剩余的牌。
为了简化实现,先做这样一个约定,玩家甲和乙手中牌的牌面值只有1-9。
测试样例(测试样例均采用输入前规定当前发牌数量的方式)
序号
输入
输出
1
请输入发牌数:6
玩家甲:2 4 1 2 5 6
玩家乙:3 1 3 5 6 4
游戏结束:玩家乙赢
玩家乙手中牌为:6 5 2 3 4 1
桌面上还有牌为:3 4 5 6 2 1
2
请输入发牌数:8
玩家甲:5 4 1 3 7 9 6 2
玩家乙:6 2 4 8 5 7 1 3
游戏结束:玩家乙赢
玩家乙手中牌为:4 1 2 4 5 7 8 3 6 5
桌面上还有牌为:9 7 6 1 2 3
3
请输入发牌数:9
玩家甲:3 4 5 6 2 1 8 7 9
玩家乙:6 5 2 3 4 1 9 7 8
游戏结束:玩家甲赢
玩家乙手中牌为:2 1 6 3 4 2 9 8 9 5 6 3 7 5
桌面上还有牌为:1 4 8 7
以下是我写的代码,有许多问题,可以帮我看看吗。
- #include<stdio.h>
- #include<stdlib.h>
- #define maxqsize 100
- #define stack_init_size 100
- #define stackincrement 10
- #define OK 1
- #define ERROR 0
- typedef int status;
- typedef struct //声明结构体Production用于表示生产日期
- { int year;
- int month;
- int day;
- }production;
- typedef struct
- { int number; //商品编号
- production date; //商品生产日期
- }selemtype;
- typedef struct{
- selemtype *base;//栈底指针
- selemtype *top;//栈顶指针
- int date[9];
- int stacksize;
- }sqstack;
- typedef struct{ //队列的结构体
- selemtype *base;
- int front;
- int rear;
- int date[100];
- }sqqueue;
- void initqueue(sqqueue &q) //队列初始化
- {
- q.base=(selemtype*)malloc(maxqsize*sizeof(selemtype));
- q.rear=q.front=0;
- }
- status enqueue(sqqueue &q,selemtype e)//入队
- {
- q.base[q.rear]=e;
- q.rear=(q.rear+1)%maxqsize;
- return OK;
- }
- status dequeue(sqqueue &q,selemtype &e)//出队
- {
- e=q.base[q.front];
- q.front=(q.front+1)%maxqsize;
- return OK;
- }
- void printq(sqqueue q)
- {
- int r=q.front;
- while(r!=q.rear)
- {printf("%d ",q.base[r]);
- r++;}
- printf("\n");
- }
- void initstack(sqstack &s)
- {
- s.base=(selemtype*)malloc(stack_init_size*sizeof(selemtype));
- s.top=s.base;
- s.stacksize=stack_init_size;
- }
- void push(sqstack &s,selemtype e)//入栈
- {
- if(s.top-s.base>=s.stacksize){
- s.base=(selemtype*)realloc(s.base,(s.stacksize+stack_init_size)*sizeof(selemtype));
- if(!s.base)
- printf("error");
- s.top=s.base+s.stacksize;
- s.stacksize+=stack_init_size;
- }
- *s.top=e;
- s.top++;
- }
- status pop(sqstack &s,selemtype &k)//出栈
- {
- int i=0;
- if(s.base==s.top){
- push(s,k);
- }
- s.top--;
- k=*s.top;
- return OK;
- }
- status gettop(sqstack s,selemtype &k)//取栈顶元素
- {
- if(s.top!=s.base){
- k=*(s.top-1);
- return OK;
- }
- return ERROR;
- }
- void print(sqstack &s)//输出栈
- {
- selemtype *e=s.base;
- int i=0;
- while(e<s.top)
- {
- printf("商品编号为%d,生产日期为%d %d %d\n",e->number,e->date.year,e->date.month,e->date.day);
- i++;
- e++;
- }
- }
- status judge(sqstack &s,selemtype e)//栈顶元素大于输入返回1
- {
- selemtype k;
- gettop(s,k);
- if(k.date.year>e.date.year){
- return OK;
- }
- else if(k.date.year==e.date.year){
- if(k.date.month<e.date.month){
- return OK;}
- else if(k.date.month==e.date.month){
- if(k.date.day>e.date.day){
- return OK;
- }
- else if(k.date.day==e.date.day){
- return OK;
- }
- }
- }
- else {
- return ERROR;
- }
- }
- void Lineup()
- {
- int i,j=1,t;
- sqstack s,temp;
- initstack(s);
- initstack(temp);
- while(j!=4)
- {
- printf("1.入库 2.出库 3.查看货物 4.退出\n");
- scanf("%d",&j);
- switch(j){
- case 1:{
- selemtype e,k;
- printf("请输入商品编号及生产日期:\n");
- scanf("%d %d %d %d",&e.number,&e.date.year,&e.date.month,&e.date.day);
- if(s.top==s.base)
- {
- push(s,e);
- }
- else{
- t=judge(s,e);
- while(t==0){
- pop(s,k);
- push(temp,k);
- t=judge(s,e);
- }
- push(s,e);
- while(temp.base!=temp.top)
- {
- pop(temp,k);
- push(s,k);
- }
- }
- break;}
- case 2: {
- selemtype w;
- pop(s,w);
- printf("该商品编号为%d,生产日期为%d %d %d\n",w.number,w.date.year,w.date.month,w.date.day);
- break;}
- case 3:{
- print(s);
- break;}
- }
- }
- }
- void game()
- {
- int n,i,b[9],t,x,j;
- sqqueue q1,q2;
- sqstack s2;
- selemtype e,k,f;
- for(i=0;i<9;i++)
- {
- b[i]=0;
- }
- initqueue(q1);
- initqueue(q2);
- initstack(s2);
- printf("请输入发牌数:(1-9)");
- scanf("%d",&n);
- printf("玩家甲的牌为:");
- for(i=0;i<n;i++){
- scanf("%d",&e);
- enqueue(q1,e);
- }
- printq(q1);
- printf("玩家乙的牌为:");
- for(j=0;j<n;j++){
- scanf("%d",&e);
- enqueue(q2,e);
- }
- printq(q2);
- int a=1;
- printf("%d",q1.date[q1.front]);
- while(q1.front!=q1.rear&&q2.front!=q2.rear)
- {
- if(a==1){
- dequeue(q1,e);
- t=q1.front;
- a=0;
- }
- else
- {
- dequeue(q2,e);
- t=q2.front;
- a=1;
- }
- if(b[t]==1){
- if(a==0){
- enqueue(q1,e);
- while(t!=x){
- pop(s2,k);
- x=(int)s2.top-1;
- b[x]=0;
- enqueue(q1,k);
- }
- }
- else
- enqueue(q2,e);
- while(t!=x){
- pop(s2,k);
- x=(int)s2.top-1;
- b[x]=0;
- enqueue(q2,k);
- }
- }
- else
- b[t]=0;
- }
- if(q1.front==q1.rear){
- printf("游戏结束:玩家乙获胜!\n");
- printf("玩家乙手中牌为:");
- printq(q2);
- }
- else
- printf("游戏结束:玩家甲获胜!\n");
- printf("玩家甲手中牌为:");
- printq(q2);
- printf("桌面上剩的牌为:");
- print(s2);
- }
- int main()
- {
- Lineup();
- game();
- return 0;
- }
复制代码 |
|