读书人

算法

发布时间: 2012-04-23 13:17:38 作者: rapoo

求一个算法
有17个人围成一个圈,编号依次是1-17,从1号开始,数1,2,3!数到3的则从圈子出去,如3,6,9出局……如此循环,问最后剩下的人的编号是多少?


这个用C语言如何实现?

[解决办法]
搜索约瑟夫环
有很多例子的
[解决办法]
单项循环链表
[解决办法]

C/C++ code
/*1:输出单向循环链表的所有结点的值2:查找第i个结点或值为某一个值的结点3:在指定的地方插入一个结点4:删除指定的结点5: 退出系统*/#include "stdio.h"#include "stdlib.h"#include "string.h"#include "malloc.h"#include "iostream.h"#define DATATYPE charstruct LINKLIST{DATATYPE data;struct LINKLIST *next;};void print(struct LINKLIST *head) //输出链表元素{struct LINKLIST *p;//定义一个指针p=head->next;cout<<"输出元素值:";while(p!=head){cout<<p->data<<" ";p=p->next;}}void get(int i,struct LINKLIST *head)//按序号查找{struct LINKLIST *p;int j;j=0;p=head;while((j<i)&&(p->next!=head)){p=p->next;j++;}if(j==i)cout<<"您要找的第"<<i<<"个元素是:"<<p->data;elsecout<<"没有找到您要找的元素!";}struct LINKLIST * locat(DATATYPE x,struct LINKLIST *head)//按值查找元素{struct LINKLIST *p;p=head->next;while(p!=head){if(p->data==x){ return p;}else{p=p->next;}}if(p==head){ return NULL;}}void insertafter(DATATYPE insertdata,struct LINKLIST *p)//在已知结点之后插入一个结点{LINKLIST *t; if(p){t=(struct LINKLIST *)malloc(sizeof(struct LINKLIST));t->data=insertdata;t->next=p->next;p->next=t;}}int deletafter(struct LINKLIST *head,struct LINKLIST *p) // 删除后继结点 {struct LINKLIST *t;int r=1;if(p->next!=head){t=p->next;p->next=t->next;free(t); }else r=0;return r;}int deletself(struct LINKLIST *head,struct LINKLIST *p) //删除结点本身{LINKLIST *q;q=head;if(!p)return 0;elsewhile(q->next!=p && q->next!=head)q=q->next;q->next=p->next;free(p);return 1;}void main(){int i;LINKLIST *p;LINKLIST *head,*t,*last;// p=head;t=( LINKLIST *)malloc(sizeof(LINKLIST));//单链表的建立(尾插入法)char ch,insertdata;head=t;last=t;t->next=head;printf("单链表元素为单个字符,连续输入(不要空格),以“0”为结束标志:\n");while((ch=getchar())!='0'){t=( LINKLIST *)malloc(sizeof(LINKLIST));t->data=ch;t->next=head;last->next=t;last=t;} cout<<endl<<"/*********************************************/";cout<<endl<<"/*************单向循环链表的操作**************/";cout<<endl<<"/*********************************************/";cout<<endl<<"/******************欢迎使用!*****************/";cout<<endl<<"/*********************************************/";cout<<endl<<"/*************制作者:xxyshow115、just、ㄣ黑铯礼菔******/";cout<<endl<<"/*********************************************/";cout<<endl<<endl<<"1………………………输出链表元素";cout<<endl<<"2………………………按序号查找元素";cout<<endl<<"3………………………按值查找元素";cout<<endl<<"4………………………在指定结点后插入元素";cout<<endl<<"5………………………删除后继结点";cout<<endl<<"6………………………删除结点本身";cout<<endl<<"7………………………退出系统";do{cout<<endl<<"请选择您要执行的操作:";cin>>i;switch(i){ case 1:print(head);break;case 2:int j;cout<<"请输入你要查找的序号:";cin>>j;get(j,head);break;case 3:cout<<"请输入你要查找的字符:";cin>>ch;p=locat(ch,head);if(p)cout<<"已经找到你要的元素:"<<p->data;elsecout<<endl<<"无此结点!"; break; case 4: cout<<"在哪个结点后插入:";cin>>ch;p=locat(ch,head);if(!p)cout<<endl<<"您输入的值不存在!";else{cout<<"插入结点值:";cin>>insertdata;insertafter(insertdata,p);cout<<endl<<"操作后的链表为:";print(head);}break;case 5: cout<<"删除哪个结点的后继:";cin>>ch;p=locat(ch,head);if(!p)cout<<endl<<"您输入的值不存在!";else{if(deletafter(head,p))cout<<"删除成功!";cout<<"操作后的链表为:";print(head);}break;case 6: cout<<"删除哪个结点:";cin>>ch;p=locat(ch,head);if(!p)cout<<endl<<"您输入的值不存在!";else{if(deletself(head,p))cout<<"删除成功!";cout<<"操作后的链表为:";print(head);}break;case 7:exit(0);}}while(i!=7);} 


[解决办法]

C/C++ code
#include<stdio.h>int main (){ int cycle[18],pos=1,i;//pos是查数人的位置,这里位置既是编号     cycle[0]=17;//存放剩余的人数 for( i=1;i<17;i++)//生成自连接链表,比如cycle【2】=3即编号为2的人的下个人的编号     cycle[i]=i+1;     cycle[i]=1;//连接成圈     while(cycle[0]!=1)//剩余一人退出     {         int location_of_r2=cycle[pos];//pos为%3余1的编号,此语句读取%3余2的编号         pos=cycle[location_of_r2]=cycle[cycle[location_of_r2]];//让余2的编号指向下一个余1的编号,即跳过%3==0的编号         cycle[0]--;         pos%=17;         }     printf("\n%d\n",pos); return 0;}
[解决办法]
C/C++ code
#include <STDIO.H>int main(){    bool flag[17];    int i,count,out=0;    //init     for (i=0;i<17;i++)    {        flag[i]=true;    }    i=0;    count=0;    while (out<16)    {        if (flag[i])        {            count++;            if (count==3)            {                flag[i]=false;                out++;                count=0;            }        }        i=(i+1)%17;    }    //    for (i=0;i<17;i++)    {        if (flag[i])        {            printf("%d\n",i+1);            break;        }    }    return 0;    } 

读书人网 >C语言

热点推荐