icpc:problems:luogup1996
problems | |
---|---|
名称 | 约瑟夫问题 |
题目编号 | P1996 |
题目链接 | luogu.com.cn/… |
来源 | Luogu |
算法分类 | 链表, 队列 |
难易程度 | 入门 |
约瑟夫问题
想法
用链表记录每个元素的后一个元素的位置,然后进行模拟。
代码实现
#include<cstdio> struct node{ int val, end; }; node line[110]; void cut(int x) { int e = line[x].end; line[f].end = e; return ; } int main() { int n, m; scanf("%d %d", &n, &m); for(int i=1; i<=n; ++i) { line[i].val = i; line[i].end = i+1; } line[n].end = 1; int now = 1, tot = n, cnt = 1; while( tot > 0 ) { if(cnt == m) { printf("%d ", line[now].val); cut(now); cnt = 1; tot--; } else { cnt ++; } now = line[now].end; } return 0; }
/app/www/public/data/pages/icpc/problems/luogup1996.txt · 最后更改: 2024/03/14 00:48 由 温婕莺