| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 312 人关注过本帖
标题:求助,帮忙看看这个程序怎么完善,有错误运行不出来
收藏  订阅  推荐  打印
llraul26
Rank: 1
等级:新手上路
帖子:1
积分:112
注册:2008-6-15
求助,帮忙看看这个程序怎么完善,有错误运行不出来

这是一道约瑟夫环的问题,编号为1,2,....,N的N个人按顺时针方向围坐一圈,每人持有一个密码(正整数),一开始任选一个正整数作为报数上限值M,从第一个 人开始按顺时针方向自1开始顺序报数,报到M时停止报数。报M的人出列,将他的密码作为新的M值,从他在顺时针方向上的下一个人开始重新从1报数,如此下 去,直至所有人全部出列为止。


#include <iostream.h>
#include <stdio.h>
#define MAX_NODE_NUM 100
#define TRUE 1U
#define FALSE 0U
typedef struct NodeType
{
      int id;
    int cipher;
           struct NodeType *next;
} NodeType;

/* 创建单向循环链表 */
static void CreaList(NodeType **, const int);
/* 运行"约瑟夫环"问题 */
static void StatGame(NodeType **, int);
/* 打印循环链表 */
static void PrntList(const NodeType *);
/* 得到一个结点 */
static NodeType *GetNode(const int, const int);
/* 测试链表是否为空, 空为TRUE,非空为FALSE */
static unsigned EmptyList(const NodeType *);

int main(void)
{
     int n, m;
            NodeType *pHead = NULL;
            while (1)
            {
                printf("请输入人数n(最多%d个): ", MAX_NODE_NUM);
                scanf("%d", &n);
                printf("和初始密码m: ");
                scanf("%d", &m);
                if (n > MAX_NODE_NUM)
                {
                    printf("人数太多,请重新输入!\n");
                    continue;
                }
                else
                    break;
            }
            CreaList(&pHead, n);
            printf("\n------------ 循环链表原始打印 -------------\n");
            PrntList(pHead);
            printf("\n-------------删除出队情况打印 -------------\n");
            StatGame(&pHead, m);
}

static void CreaList(NodeType **ppHead, const int n)
{
            int i, iCipher;
            NodeType *pNew, *pCur;
            for (i = 1; i <= n; i++)
            {
                printf("输入第%d个人的密码: ", i);
                scanf("%d", &iCipher);
                pNew = GetNode(i, iCipher);
                if (*ppHead == NULL)
                {
                    *ppHead = pCur = pNew;
                    pCur->next = *ppHead;
                }
                else
                {
                    pNew->next = pCur->next;
                    pCur->next = pNew;
                    pCur = pNew;
                }
            }
            printf("完成单向循环链表的创建!\n");
}

static void StatGame(NodeType **ppHead, int iCipher)
{
            int iCounter, iFlag = 1;
            NodeType *pPrv, *pCur, *pDel;
            pPrv = pCur = *ppHead;
            /* 将pPrv初始为指向尾结点,为删除作好准备 */
            while (pPrv->next != *ppHead)
                pPrv = pPrv->next;
            while (iFlag)
            {
                for (iCounter = 1; iCounter < iCipher; iCounter++)
                {
                    pPrv = pCur;
                    pCur = pCur->next;
                }
                if (pPrv == pCur)
                    iFlag = 0;
                pDel = pCur; /* 删除pCur指向的结点,即有人出列 */
                pPrv->next = pCur->next;
                pCur = pCur->next;
                iCipher = pDel->cipher;
                printf("第%d个人出列, 密码: %d\n", pDel->id, pDel->cipher);
                free(pDel);
            }
            *ppHead = NULL;
            getchar();
}

static void PrntList(const NodeType *pHead)
{
            const NodeType *pCur = pHead;
            if (EmptyList(pHead))
                return;
            do
            {
                printf("第%d个人, 密码: %d\n", pCur->id, pCur->cipher);
                pCur = pCur->next;
            } while (pCur != pHead);
            getchar();
}

static NodeType *GetNode(const int iId, const int iCipher)
{
            NodeType *pNew;
            pNew = (NodeType *)malloc(sizeof(NodeType));
            if(!pNew)
            {
                printf("Error, the memory is not enough!\n");
                exit(-1);
            }
            pNew->id = iId;
            pNew->cipher = iCipher;
            pNew->next = NULL;
            return pNew;
}

static unsigned EmptyList(const NodeType *pHead)
{
            if(!pHead)
            {
                printf("The list is empty!\n");
                return TRUE;
            }
            return FALSE;
}


求助哪位大仙能够帮忙改正一下
2008-6-16 10:24
走自己路的人
Rank: 2
等级:注册会员
帖子:47
积分:624
注册:2007-4-17

包含下面这两个头文件
#include <malloc.h>
#include <stdlib.h>
再在main函数里最后加入
return 0;
就可以了

2008-6-17 22:50
共有 311 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.047602 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved