新手求教: josephus问题

xinghuan1 2016-02-15 10:15:46
题目要求调用函数 void count_list(struct node *head,int n)来输出退出圈子人的序号及最后留下人的序号(*head是单循环链表)。
我写的代码如下:
void count_list(Node *head,int m)
{
int i=1;
Node *p,*p1,*p2;
p=head;
while(?)
{
if(p==NULL)
{
p=head;
}
if(i%m==0)
{
printf("the %d left\n",p->data);
p1->next=p->next;
p2=p1;
p1=p;
p=p->next;
free(p1);
p1=p2;
}
else
{
p1=p;
p=p->next;
}
i=i+1;
}
printf("the last one is %d\n",head->data);
}
求教:怎么判断链表中只剩下一个元素。
...全文
193 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-02-16
  • 打赏
  • 举报
回复
仅供参考:
//假设有n个人团团围做,从第1个人开始数数,数到第m个人时候,第m个人出列,
//然后继续从1开始数数,数到第m个人退出
#include <stdio.h>
#include <conio.h>
int i,k,t;
int n,m;
static char f[1001];//0该座位未出圈,1该座位已出圈
void main() {
    while (1) {
        printf("Input n m(1000>=n>=m>=1):");
        fflush(stdout);
        rewind(stdin);
        if (2==scanf("%d%d",&n,&m)) {
            if (1000>=n && n>=m && m>=1) break;
        }
    }
    t=0;//已出圈总人数
    i=1;//座位编号
    k=1;//当前要数的数
    while (1) {
        if (0==f[i]) {
            if (m==k) {
                t++;
                f[i]=1;
                printf("%3d ",i);
                if (0==t%10) printf("\n");
                if (t>=n) break;
            }
            k++;if (k>m) k=1;
        }
        i++;if (i>n) i=1;
    }
    cprintf("Press any key ...");
    getch();
}
fly_dragon_fly 2016-02-16
  • 打赏
  • 举报
回复
while(head->next) m=1, p1没初始化就在使用了
ooolinux 2016-02-16
  • 打赏
  • 举报
回复
链表是一个环吗? next是自己本身就是只剩一个元素了 head结点不在环内
This second edition of Data Structures Using C has been developed to provide a comprehensive and consistent coverage of both the abstract concepts of data structures as well as the implementation of these concepts using C language. It begins with a thorough overview of the concepts of C programming followed by introduction of different data structures and methods to analyse the complexity of different algorithms. It then connects these concepts and applies them to the study of various data structures such as arrays, strings, linked lists, stacks, queues, trees, heaps, and graphs. The book utilizes a systematic approach wherein the design of each of the data structures is followed by algorithms of different operations that can be performed on them, and the analysis of these algorithms in terms of their running times. Each chapter includes a variety of end-chapter exercises in the form of MCQs with answers, review questions, and programming exercises to help readers test their knowledge. Table of Contents Chapter 1. Introduction to C Chapter 2. Introduction to Data Structures and Algorithms Chapter 3. Arrays Chapter 4. Strings Chapter 5. Structures and Unions Chapter 6. Linked Lists Chapter 7. Stacks Chapter 8. Queues Chapter 9. Trees Chapter 10. Efficient Binary Trees Chapter 11. Multi-way Search Trees Chapter 12. Heaps Chapter 13. Graphs Chapter 14. Searching and Sorting Chapter 15. Hashing and Collision Chapter 16. Files and Their Organization Appendix A: Memory Allocation in C Programs Appendix B: Garbage Collection Appendix C: Backtracking Appendix D: Josephus Problem Appendix E: File Handling in C Appendix F: Address Calculation Sort Appendix G: Answers

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧