約瑟夫環 poj 3750 小孩報數問題 類比,poj3750

來源:互聯網
上載者:User

約瑟夫環 poj 3750 小孩報數問題 類比,poj3750

Language:Default小孩報數問題
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10071   Accepted: 4702

Description

有N個小孩圍成一圈,給他們從1開始依次編號,現指定從第W個開始報數,報到第S個時,該小孩出列,然後從下一個小孩開始報數,仍是報到S個出列,如此重複下去,直到所有的小孩都出列(總人數不足S個時將迴圈報數),求小孩出列的順序。

Input

第一行輸入小孩的人數N(N<=64) 
接下來每行輸入一個小孩的名字(人名不超過15個字元) 
最後一行輸入W,S (W < N),用逗號","間隔

Output

按人名輸出小孩按順序出列的順序,每行輸出一個人名

Sample Input

5XiaomingXiaohuaXiaowangZhangsanLisi2,3

Sample Output

ZhangsanXiaohuaXiaomingXiaowangLisi

Source


題意不多說,就是普通的約瑟夫環,要求輸出出圈順序,資料比較小,N<=64,直接類比即可。

各種小問題,調了好長時間,太菜~~

代碼:

#include <iostream>#include <cstdio>using namespace std;int vis[70];char name[70][20];int n,w,s;int main(){    int i,j;    while (~scanf("%d",&n))    {        memset(vis,0,sizeof(vis));        for (i=0;i<n;i++)            scanf("%s",name[i]);        scanf("%d,%d",&w,&s);        w--;        for (i=1;i<=n;i++)        {            int num=0,a;            while (num<s)            {                if (!vis[w])                {                    a=w;                    w++;                    num++;                }                else                    w++;                w=w%n;            }            printf("%s\n",name[a]);            vis[a]=1;        }    }    return 0;}



小孩報數問題

//定義小孩數量,暫訂為20 #define m 5 //報數 typedef struct monkey {約瑟夫環問題,自己探索啦。。。 約瑟夫環問題,自己探索啦。 #
 
小孩報數問題(使用單鏈表)

#include<stdio.h>
#include<stdlib.h>
struct Node{
char name[16];
struct Node *next;
};

struct Node *head;
int creatlist(int n)
{
struct Node *tmp,*p;
int i=0;
head=(struct Node *)malloc(sizeof(struct Node));
p=head;
for(i=1;i<=n;i++)
{
tmp=(struct Node *)malloc(sizeof(struct Node));
scanf("%s",tmp->name);
p->next=tmp;
p=tmp;
}
tmp->next=head->next; //最後一個節點next指標指向第一個節點
return 0;
}

int outline(int s,int w)
{
struct Node *p,*q,*o;
int i;
p=head->next;
for(i=1;i<s;i++)//你的代碼沒什麼大的問題,就是這裡可能失誤寫錯了吧
p=p->next;
while(p->next!=p)
{
for(i=1;i!=w;i++)
{
q=p;
p=p->next;
}//要刪除的就是現在的p
o=p->next;
q->next=p->next;
printf("%s\n",p->name);
free(p);
p=o;
}

printf("%s\n",p->name);
free(p);
free(head);
return 0;
}

int main()
{
int n,s,w;
scanf("%d",&n);
creatlist(n);
scanf("%d,%d",&s,&w);
outline(s,w);
return 0;

}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.