编程实现: 问题描述: 球钟问题: 球钟是一个利用球的移动来记录时间的简单装置。 它有三个可以容纳若干个球的指示器:分钟指示器,五分钟指示器,小时指示器。若分钟指示器中有2个球,五分钟指示器中有6个球,小时指示器中有5个球,则时间为5:32。球钟的工作原理如下:分钟指示器最多可容纳4个球。每过一分钟,球钟就会从球队列的队首取出一个球放入分钟指示器,当放入第五个球时,在分钟指示器的4个球就会按照他们被放入时的相反顺序加入球队列的队尾。而第五个球就会进入五分钟指示器。按此类推,五分钟指示器最多可放11个球,小时指示器最多可放11个球。当小时指示器放入第12个球时,原来的11个球按照他们被放入时的相反顺序加入球队列的队尾,然后第12个球也回到队尾。这时,三个指示器均为空,回到初始状态,从而形成一个循环。因此,该球钟表示时间的范围是从0:00到11:59。现设初始时球队列的球数为x(27≤x≤127),球钟的三个指示器初态均为空。问要经过多少天(每天24小时),球钟的球队列才能回复原来的顺序。 要求: 分别计算球的个数x从27到127时,分别需要经过多少天,并把计算结果(以整数形式)保存在一个文件result.txt中 测试用例: 27 30 115 … 测试结果: … ================================================= #include
#include
#define TRUE 1 #define FALSE 0 typedef struct StackNode { char data; struct StackNode *next; }Stack_t; typedef struct SqNode { char data; struct SqNode *next; }Sq_t; typedef struct { Sq_t *pSq; Sq_t *pTail; }SqLink; Stack_t *CreatStack(void) { //Stack_t Stack; Stack_t *pStack = (Stack_t *)malloc(sizeof(Stack_t)); pStack->data = 0; pStack->next = NULL; return pStack; } void DestroyStack(Stack_t *pStackN) { Stack_t *pTmp = NULL; do { pTmp = pStackN; pStackN = pStackN->next; free(pTmp); pTmp = NULL; }while(pStackN->next != NULL); free(pStackN); pStackN = NULL; } void Push(Stack_t *pStackN, int iData, int *iStackCount) { Stack_t *pNewNode = (Stack_t *)malloc(sizeof(Stack_t)); if (pNewNode == NULL) { printf("mem allocate wrong/n"); exit(1); } pNewNode->data = iData; pNewNode->next = pStackN; pStackN = pNewNode; iStackCount++; } int Pop(Stack_t *pStackN, int *iStackCount) { if (pStackN->next == NULL) { printf("the stack is empty!!!/n"); return -1; } int iData = pStackN->data; Stack_t *pTmp = pStackN; pStackN = pStackN->next; free(pTmp); pTmp = NULL; iStackCount--; return iData; } int isFull() { } int isEmpty() { } //===================================================================StackEndl================= SqLink *CteateSq(int size) { SqLink *pSqLink; pSqLink->pSq = (Sq_t *)malloc(size * sizeof(Sq_t)); pSqLink->pTail = pSqLink->pSq; pSqLink->pTail->next = NULL; int i = 0; for (i = 0; i < size; i++) { Insert(&pSqLink, i); } return pSqLink; } void DestroySq(SqLink **pSqLink) { Sq_t *pTmp; while(((*pSqLink)->pSq) != NULL) { pTmp = (*pSqLink)->pSq; (*pSqLink)->pSq = (*pSqLink)->pSq->next; free(pTmp); } pTmp = NULL; } void Insert(SqLink **pSqLink, char e) { Sq_t *pNewNode = (Sq_t *)malloc(sizeof(Sq_t)); pNewNode->data = e; pNewNode->next = NULL; (*pSqLink)->pTail->next = pNewNode; (*pSqLink)->pTail = pNewNode; } void Delete(SqLink **pSqLink, char *e) { Sq_t *pTmp = (*pSqLink)->pSq->next; *e = pTmp->data; (*pSqLink)->pSq = pTmp->next; free(pTmp); pTmp = NULL; return ; } //============================= int ReadIn() { static inputData[150] = {-1}; static nIndex = 0; static isFirst = TRUE; int i = 0; FILE *fp = NULL; if (isFirst) { fp = fopen("D://test.txt","r"); for (i = 0; i < 150; i++) { if ((fscanf(fp, "%d", &inputData[i])) == EOF) break; } isFirst = FALSE; fclose(fp); } return inputData[nIndex++]; } void WriteOut(int *iDays) { FILE *fp = NULL; fp = fopen("D://ballclock.txt", "w"); int i = 0; while(iDays[i] != 0) { //printf("data = %d/n", i); fprintf(fp, "%d/t", iDays[i]); i++; } fclose(fp); } int aiData[150]; int main() { Stack_t *pStackO = CreateStack(); Stack_t *pStackF = CreateStack(); Stack_t *pStackH = CreateStack(); int i = 0; SqLink *pSqLink = NULL; //pSqLink = CreateStack(); static int iStackCountO = 0; static int iStackCountF = 0; static int iStackCountH = 0; for (i = 0; i < 101; i++) { aiData[i] = ReadIn(); //if (aiData[i] == 127) //break; } int m; //while(1) int iTxtData; int iDeleteData; for (j = 0; j < 101; j++) { //aiData[j] = ReadIn(); iTxtData = ReadIn(); pSqLink = CreateSq(iTxtData); //===================================== //do Delete(&pSqLink,&iDeleteData); Push(pStackO,iDeleteData,&iStackCountO); if (iStackCountO == 5)//TestStackOne { for (i = 0; i < 4; i++) { Insert(&pSqLink,Pop(pStackO,iStackCountO)); } Push(pStackF,iDeleteData,&iStackCountF); if(iStackCountF == 12)//TestStackFive { for (i = 0; i < 12; i++) { Insert(&pSqLink,Pop(pStackF,iStackCountF)); } Push(pStackH, iDeleteData, &iStackCountH); if (isStackCountH == 12) { for (i = 0; i < 11; i++) { Insert(&pSqLink,Pop(pStackH,iStackCountH)); } Insert(&pSqLink,iDeleteData));//此时为一天结束; iDay++; iaDays[iTxtData - 26] = iDay; //===========================judge it for (i = 0; i < iTxtData i++) { get(&pSqLink, m);//每次去的一个队列数据进行比较。 if(m == i ) { iCount++; } if (iCount == iTxtData) { //success; } } //============================ } } } //============================================== } WriteOut(aiData); return 0; }
球钟问题
最新推荐文章于 2022-07-24 19:12:46 发布