数据结构-球钟问题

球钟问题

一、题目

球钟是一个利用球的移动来记录时间的简单装置
他有三个可以容纳的若干球的指示器:分钟指示器,五分种指示器,小时指示器。若分钟指示器中有2个球,五分钟指示器中有六个球,小时指示器中有5个球。时间为5:32。
每过一分钟,球钟就会从球队列的队首取出一个分钟指示器,分钟指示器最多可以容纳4个球。
当放入五个球时,在分钟指示器的4个球就会按照他们按放入的相反顺序加入球队列的对位,而第五个球就会进入五分钟指示器。一次类推,五分钟指示器最多放11个球,小时指示器最多放11球。
当小时指示器放入第12个球时,原来的11个球按照他们被放入时的相反顺序加入到队列的队尾,然后第12个球也会回到队尾。
这时三个指示器均为空,回到初始状态,从而形成一个循环。因此该球钟表示时间范围0:00到11:59。
现设初始时球队列球数位27,球钟的三个指示器初始状态为空,问要经过多久,球队列才能回到原来的顺序。

2、实现

linkqueue.h

typedef int datatype;

typedef struct node {
	datatype data;
	struct node *next;
}listnode , *linklist;

typedef struct {
	linklist front;
	linklist rear;
}linkqueue;

linkqueue * queue_create();
int enqueue(linkqueue *lq, datatype x);
datatype dequeue(linkqueue *lq);
int queue_empty(linkqueue *lq);
int queue_clear(linkqueue *lq);
linkqueue * queue_free(linkqueue *lq);

linkqueue.c

#include <stdio.h>
#include <stdlib.h>
#include "linkqueue.h"

linkqueue * queue_create() {
	linkqueue *lq;

	if ((lq = (linkqueue *)malloc(sizeof(linkqueue))) == NULL) {
		printf("malloc linkqueue failed\n");
		return NULL;
	}

	lq->front = lq->rear = (linklist)malloc(sizeof(listnode));
	if (lq->front == NULL) {
		printf("malloc node failed\n");
		return NULL;
	}
	lq->front->data = 0;
	lq->front->next = NULL;

	return lq;
}

int enqueue(linkqueue *lq, datatype x) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

	if ((p = (linklist)malloc(sizeof(listnode))) == NULL) {
		printf("malloc node failed\n");
		return -1;
	}
	p->data = x;
	p->next = NULL;

	lq->rear->next = p;
	lq->rear = p;

	return 0;
}

datatype dequeue(linkqueue *lq) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

	p = lq->front;
	lq->front = p->next;
	free(p);
	p = NULL;

	return (lq->front->data);
}

int queue_empty(linkqueue *lq) {
	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

	return (lq->front == lq->rear ? 1 : 0);
}

int queue_clear(linkqueue *lq) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

	while (lq->front->next) {
		p = lq->front;
		lq->front = p->next;
		printf("clear free:%d\n", p->data);
		free(p);
		p = NULL;
	}
	return 0;
}

linkqueue * queue_free(linkqueue *lq) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return NULL;
	}

	while (lq->front) {
		p = lq->front;
		lq->front = p->next;
		printf("free:%d\n", p->data);
		free(p);
	}

	free(lq);
	lq = NULL;

	return NULL;
}

sqstack.h

typedef int data_t;

typedef struct {
	data_t *data;
	int maxlen;
	int top;
}sqstack;

sqstack * stack_create(int len);
int stack_push(sqstack * s, data_t value);
int stack_empty(sqstack *s);
int stack_full(sqstack *s);
data_t stack_pop(sqstack *s);
data_t stack_top(sqstack *s);
int stack_clear(sqstack *s);
int stack_free(sqstack *s);

sqstack.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqstack.h"

sqstack * stack_create(int len) {
	sqstack * s;

	if ((s =(sqstack *)malloc(sizeof(sqstack))) == NULL) {
		printf("malloc sqstack failed\n");
		return NULL;
	}

	if ((s->data = (data_t *)malloc(len * sizeof(data_t)))==NULL) {
		printf("malloc data failed\n");
		free(s);
		return NULL;
	}

	memset(s->data, 0, len*sizeof(data_t));
	s->maxlen = len;
	s->top = -1;

	return s;
}

int stack_push(sqstack * s, data_t value) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}

	if (s->top == s->maxlen-1) {
		printf("stack is full\n");
		return -1;
	}

	s->top++;
	s->data[s->top] = value;

	return 0;
}

/*
 *@ret 1-empty
 * */
int stack_empty(sqstack *s) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}
	return (s->top == -1 ? 1 : 0);
}

/*
 * @ret 1-full
 * */
int stack_full(sqstack *s) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}
	return  (s->top == s->maxlen-1 ? 1 : 0);
}

data_t stack_pop(sqstack *s) {
	s->top--;
	return (s->data[s->top+1]);
}

data_t stack_top(sqstack *s) {
	return (s->data[s->top]);
}

int stack_clear(sqstack *s) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}
	
	s->top = -1;
	return 0;
}

int stack_free(sqstack *s) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}
	
	if (s->data != NULL) 
		free(s->data);
	free(s);

	return 0;
}

test.c

#include <stdio.h>
#include "linkqueue.h"
#include "sqstack.h"

int check(linkqueue * lq);

int main(int argc, const char *argv[])
{
	linkqueue *lq;
	sqstack *s_hour, *s_five, *s_min;
	int value;
	int i, min = 0;

	if ((lq = queue_create()) == NULL) {
		return -1;
	}

	for (i = 1; i <= 27; i++) {
		enqueue(lq, i);
	}

	if ((s_hour = stack_create(11)) == NULL) {
		return -1;
	}

	if ((s_five = stack_create(11)) == NULL) {
		return -1;
	}

	if ((s_min = stack_create(4)) == NULL) {
		return -1;
	}

	while (1) {
		min++;
		if (!queue_empty(lq)) {
			value = dequeue(lq);
			if (!stack_full(s_min)) {
				stack_push(s_min, value);
			} else {
				while (!stack_empty(s_min)) {
					enqueue(lq, stack_pop(s_min));
				}
				if (!stack_full(s_five)) {
					stack_push(s_five, value);
				} else {
					while (!stack_empty(s_five)) {
						enqueue(lq, stack_pop(s_five));
					}
					if (!stack_full(s_hour)) {
						stack_push(s_hour, value);
					} else {
						while (!stack_empty(s_hour)) {
							enqueue(lq, stack_pop(s_hour));
						}
						enqueue(lq, value);
						//0:0
						if (check(lq) == 1) {
							break;
						}
					}
				}

			}
		}
	}
	printf("total:%d\n", min);

	printf("dequeue:");
	while (!queue_empty(lq)) 
		printf("%d ", dequeue(lq));
	
	puts("");

	return 0;
}

int check(linkqueue * lq) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

	p = lq->front->next;

	while (p && p->next) {
		if (p->data < p->next->data) {
			p = p->next;
		} else {
			return 0;
		}
	}
	return 1;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 一个可以帮助人们快速组装家具的工具。该工具可以使用简单的手动操作,无需使用任何电动工具,从而大大简化了家具组装过程。 另一个可能是一种智能灯泡,它不仅可以通过手机应用程序进行控制,还可以根据用户的生理数据(如睡眠模式)自动调整亮度和色温。 ### 回答2: 我能为您提供一些可能的实用新型专利点子,供您参考: 1. 智能家居电源管理系统:设计一个智能家居电源管理系统,能够实时监测和控制各种设备的电力消耗,根据实时需求自动调整电源供应,以提高能源利用效率并降低能源浪费。 2. 高效清洗装置:研发一种高效清洗装置,结合机械和化学原理,能够快速去除附着在各种表面的污渍,例如玻璃、墙壁、地板等,并且具有环保特性,不产生有害副产品。 3. 可穿戴智能医疗监测器:开发一种可穿戴智能医疗监测器,能够实时监测人体生理指标如心率、体温、血压等,并将数据传输至移动设备,帮助用户及时了解自身健康状况。 4. 汽车智能停车系统:设计一种汽车智能停车系统,通过使用传感器和自动控制技术,能够帮助驾驶员寻找空余停车位并辅助完成停车动作,提高停车效率,并减少停车事故的发生。 5. 智能应急全装置:发明一种智能应急全装置,结合传感器和通信技术,能够主动感知并报警各种紧急情况,例如火灾、气体泄漏等,同时将报警信息发送给相关人员,提高应急处理效率。 这些是一些可能的实用新型专利点子,但需要注意的是在实施之前需要进行详细的市场调研和技术验证,以确保这些点子的可行性和市场需求。 ### 回答3: 我帮您想了一个实用新型专利的点子:智能健康手环。 智能健康手环是一种结合了健康管理和智能科技的创新产品。手环内置了多种传感器,可以实时监测用户的健康数据,包括心率、血压、睡眠状态等。同时,手环还具备智能指导功能,可以根据用户的健康数据给出相应的建议和提醒,帮助用户更好地管理自己的健康。 与传统的健康手环相比,这个新型专利的手环有以下几个特点: 1. 多功能监测:手环内置的传感器可以同时监测多个健康数据,不仅可以监测运动数据,还可以监测心率、血压等身体指标,帮助用户全面了解自己的健康状况。 2. 智能指导:根据用户的个人健康数据和设定目标,手环可以给出相应的建议和指导,比如定期提醒用户锻炼、保持饮食平衡等,帮助用户改善生活习惯,提高健康水平。 3. 数据分析与应用:手环可以将用户的健康数据进行存储和分析,并通过手机APP进行展示和管理。用户可以随时查看自己的健康数据和历史记录,了解自己的健康趋势和改善情况。 4. 智能联动:手环可以与其他智能设备进行联动,比如智能手表、智能手机等,实现更全面的数据收集和交互。用户可以通过智能手环管理自己的健康,实现全方位的智能化健康管理。 通过以上的特点,这款智能健康手环能够有效帮助用户管理自己的健康,并提供个性化的健康服务。未来随着人们对健康的关注度增加,这款手环有着广阔的市场前景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值