---
title: "Josephus问题.cpp"
author: "mmdev"
date: "2010-12-03T19:36:00Z"
tags: []
slug: "josephus-cpp"
draft: false
lastmod: "2011-11-11T12:54:14Z"
id: 1253404
url: "https://mmdev.iteye.com/blog/1253404"
---

#include&lt;iostream&gt;  
using namespace std;  
const int number=3; //假如报3的人退出圈子  
const int n=5; // 假如人数为5

int main()  
{  
int a\[n];  
int i;  
int k=1,j;  
for(i=0;i&lt;n;i++)  
{  
a\[i]=i+1;  
cout&lt;&lt;a\[i]&lt;&lt;" ";  
}  
cout&lt;&lt;endl;  
int m=-1; // 作为数组下标  
while(true)  
{  
for(j=0;j&lt;number;)  
{  
m=(m+1)%n;  
if(a\[m]!=0)  
{  
j++;  
}  
}  
if(n==k)  
{  
break;  
}  
cout&lt;&lt;a\[m]&lt;&lt;" ";  
a\[m]=0;  
k++;  
}  
for(i=0;i&lt;n;i++)  
{  
if(a\[i]!=0)  
{  
cout&lt;&lt;"最后留下的是 "&lt;&lt;a\[i];  
}  
}  
return 0;  
}