UVa 1394: And Then There Was One
2013-08-19 20:18
387 查看
设置一个数组Winner记录经典约瑟夫问题中的剩余者即可递归解决该问题。
注: 约瑟夫问题:有编号为0~n-1的n个人,从0号开始报数1,2,3......报到k的杀死,然后从下一个人开始继续报数1,2,3......直到剩下最后一个人。约瑟夫想要活下来,问他一开始需要站在几号位置(编号为几)。
数组Winner也可以只用一个变量代替。
我的解题代码如下:
注: 约瑟夫问题:有编号为0~n-1的n个人,从0号开始报数1,2,3......报到k的杀死,然后从下一个人开始继续报数1,2,3......直到剩下最后一个人。约瑟夫想要活下来,问他一开始需要站在几号位置(编号为几)。
数组Winner也可以只用一个变量代替。
我的解题代码如下:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; //#define MAXN 10005 //int Winner[MAXN]; //Winner[i]表示约瑟夫问题中初始有i个人(编号0~i-1),最后剩余者的编号 int main() { int n,k,m; while(cin >> n >> k >> m && n!=0) { //init // Winner[1] = 0; int Winner = 0; for(int i=2; i<n; i++) { // Winner[i] = (Winner[i-1]+k)%i; Winner = (Winner+k)%i; } // cout << (Winner[n-1]+m)%n+1 << endl; cout << (Winner+m)%n+1 << endl; } return 0; }
相关文章推荐
- UVA 1394 And Then There Was One(约瑟夫环变形)
- UVA1394 And Then There Was One
- UVa 1394: And Then There Was One
- UVA - 1394 And Then There Was One
- UVa 1394 - And Then There Was One(约瑟夫数论)
- uva 1394 - And Then There Was One(约瑟夫环)
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
- UVA 1394/POJ 3517 And Then There Was One
- 【约瑟夫环变形】UVa 1394 - And Then There Was One
- UVA 1394 POJ 3517 And Then There Was One (双向循环链表和递推)
- UVa:1394 And Then There Was One
- uva 1394 - And Then There Was One
- UVa1394 - And Then There Was One
- UVA 1394 And Then There Was One
- UVALive - 3882:And Then There Was One
- uva live 3882 And Then There Was One 约瑟夫环
- UVALive 3882 And Then There Was One 约瑟夫环问题
- UVALive 3882 And Then There Was One 约瑟夫环问题
- ecjtu-summer training #5 F - And Then There Was One UVALive - 3882
- UVALive 3882 - And Then There Was One【约瑟夫问题】