N个小孩围成一圈,从第M个开始数数,数到K的小孩出列,最后出列的小孩胜出,编程求出列顺序以及最后获胜者。
在线等高手解答,最好能给出代码和思路
这是个典型约瑟夫环问题
C++数列堆实现:
#include <iostream>
#include <list>
using std::cout;
using std::endl;
using std::cin;
using std::list;
int main()
{
int total = 0;
cout << "Please input total number of people : ";
cin >> total;
int number = 0;
cout << "Please input selected number : ";
cin >> number;
/* If number = 3
* f(1) = 0
* f(2) = 1 = (f(1) + 3) % 2
* f(3) = 1 = (f(2) + 3) % 3
* f(4) = 0 = (f(3) + 3) % 4
* f(5) = 3 = (f(4) + 3) % 5
* ...
* f(n) = x = (f(n-1) + 3) % n
* */
int last = 0; // f(1) = 0
for(int i = 2; i <= total; ++i)
{
last = (last + number) % i;
}
cout << "The last one is : " << last + 1 << endl;
return 0;
}
C++非递归实现(模拟实现)
1#include
2 #include
3
4 using namespace std;
5
6 int main() {
7 int total = 0;
8 cin >> total;
9
10 int number = 0;
11 cin >> number;
12
13 list * table = new list();
14
15 for(int i = 1; i <= total; ++i) {
16 table->push_back(i);
17 }
18 int shout = 1;
19 int last = 0;
20
21 list::iterator next;
22 for(list::iterator it = table->begin() ; !table->empty();) {
23 if(shout++ == number) {
24 last = *it;
25 next = it++;;
26 table->erase(it);
27 it = next;
28 shout = 1;
29 }
30 else
31 it++;
32
33
34 if(it == table->end())
35 it = table->begin();
36 }
37
38 cout << last << endl;
39 }
~
///链表实现约瑟夫环(可编译完整代码)
#include
#include
#include
using namespace std;
int main(int argc, char* argv[])
{
int total=0;
cout<<"Please input the total num:";
cin>>total;
int keyNum=0;
cout<<"Please input the key num:";
cin>>keyNum;
if(keyNum {
cout system("paused");
}
list* table=new list();
for(int i=1; i<=total; ++i)
{
table->push_back(i);
}
int shout=1;
for(list::iterator it=table->begin(); table->size()!=1;)
{
if(shout++==keyNum)
{
//cout<<*it< it=table->erase(it);
shout=1;
}
else
{
++it;
}
if(it==table->end())
{
it=table->begin();
}
}
cout<<"The last one is:";
cout<<*table->begin()<<endl;
system("pause");
return 0;
}
#include
#include
#include
using namespace std;
int main(int argc, char* argv[])
{
int total=0;
cout<<"Please input the total num:";
cin>>total;
int keyNum=0;
cout<<"Please input the key num:";
cin>>keyNum;
if(keyNum {
cout system("paused");
}
list* table=new list();
for(int i=1; i<=total; ++i)
{
table->push_back(i);
}
int shout=1;
for(list::iterator it=table->begin(); table->size()!=1;)
{
if(shout++==keyNum)
{
//cout<<*it< it=table->erase(it);
shout=1;
}
else
{
++it;
}
if(it==table->end())
{
it=table->begin();
}
}
cout<<"The last one is:";
cout<<*table->begin()<<endl;
system("pause");
return 0;
}
猴子选大王c语言用数组实现:
http://blog.csdn.net/slingchen/article/details/50169975
///链表实现约瑟夫环(可编译完整代码)
#include
#include
#include
using namespace std;
int main(int argc, char* argv[])
{
int total=0;
cout<<"Please input the total num:";
cin>>total;
int keyNum=0;
cout<<"Please input the key num:";
cin>>keyNum;
if(keyNum {
cout system("paused");
}
list* table=new list();
for(int i=1; i<=total; ++i)
{
table->push_back(i);
}
int shout=1;
for(list::iterator it=table->begin(); table->size()!=1;)
{
if(shout++==keyNum)
{
//cout<<*it< it=table->erase(it);
shout=1;
}
else
{
++it;
}
if(it==table->end())
{
it=table->begin();
}
}
cout<<"The last one is:";
cout<<*table->begin()<<endl;
system("pause");
return 0;
}