首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一个简单的约瑟夫有关问题 老是弄错~

2012-10-14 
一个简单的约瑟夫问题 老是弄错~~关键在于寻找人的那个函数cir_find 函数写不出来 算法思想看名字都差不多

一个简单的约瑟夫问题 老是弄错~~
关键在于寻找人的那个函数cir_find 函数写不出来 算法思想看名字都差不多了吧 各位大大帮帮看下哈·~~急求 !!
谢了~~


#include<iostream>

using namespace std ;

class Node{
friend class cir_list ;
public:
Node(){ data = 0 ;} ;
Node(int) ;
~Node() ;
private:
int data ;
Node * next ;
} ;

Node::Node(int n){
data = n ;
}

Node::~Node(){
cout<<this->data<<endl ;
}


class cir_list{
public:
cir_list() ;
~cir_list(){} ;

void initList() ;
void push(int ) ;
void deleteNode(int ) ;
void findPeople() ;
private:
Node * head ;
int len ;
} ;

cir_list::cir_list(){
head = new Node() ;
head ->next = head ;
len = 0 ;
}

void 
cir_list::push(int n){
Node * current = head ;
Node * newNode ;

for(int i = 1 ; i <= n ; i++){
newNode = new Node(i) ;
newNode->next = current ->next ;
current ->next = newNode ;
current = current ->next ;
}
len = n ;
}

void 
cir_list::deleteNode(int n){
Node * current = head ->next ;
Node * trailCurrent = head ;

while(current != head && current ->data != n){
trailCurrent = current ;
current = current ->next ;
}

trailCurrent ->next = current ->next ;
delete current ;
len -- ;
}

void
cir_list::findPeople(){
Node * current = head ->next ;
int i = 0 ;
while(current ){
cout<<current->data<<" " ;
current = current ->next ;
i ++ ;
if (i == 9)
deleteNode(current ->data) ;
if(i > 20) 
break ;

}
}

int main(){
cir_list cir ;
cir.push(30) ;
cir.findPeople() ;

system("pause") ;
}

[解决办法]
给你参考下,现在有事等会回来看(这个星期时间很紧)

C/C++ code
#include<stdio.h>#include<stdlib.h>typedef struct node{   int a;   int b;   struct node *next;   }Lnode;Lnode *creatlist(int n);void printlist(Lnode *p);void fun(Lnode *p,int n,int m);main(){     Lnode *p;   int n,m;   printf("input n and m:\n");   scanf("%d,%d",&n,&m);   p=creatlist(n);   printf("the list is:\n");   printlist(p);   printf("\nchu lie sunxu:\n");   fun(p,n,m);}Lnode *creatlist(int n){ int i;   Lnode *head,*p,*s;   head=( Lnode *) malloc(sizeof(Lnode ));   p=head;   head->next=p;   printf("enter a,b:\n");   scanf ("%d,%d",&p->a,&p->b);   for (i=2; i<=n; ++i )   { s=(Lnode *) malloc(sizeof(Lnode ));   scanf ("%d,%d",&s->a,&s->b);   s->next=head;   p->next=s; p=s;   }   return (s);}void printlist(Lnode *p){ Lnode *q;   q=p->next;   while(q!=p)   { printf("%3d",q->b);   q=q->next;   }   printf("%3d",p->b);}void fun(Lnode *p,int n,int m){ Lnode *q; int i,count; q=p; for(i=1;i<n;i++) {   count=0;   if(i!=1)     count++;   while(count!=m)     {       p=q;       q=q->next;       count++;     }     printf("%3d",q->a);     m=q->b;     p->next=q->next; free(q);     q=p->next; } printf("%3d",p->a);}
[解决办法]
n为长度 m为从哪个人开始 fun()里面的是具体的思路
[解决办法]
http://www.cnblogs.com/_programmer/archive/2009/09/17/1568481.html还有这个(不同实现方法) 里面有分析
------解决方案--------------------


仅供参考

C/C++ code
//假设有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();} 

热点排行