当前位置:
首页 >
51nod 1073 约瑟夫环
发布时间:2025/7/25
46
豆豆
生活随笔
收集整理的这篇文章主要介绍了
51nod 1073 约瑟夫环
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
N个人坐成一个圆环(编号为1 - N),从第1个人开始报数,数到K的人出列,后面的人重新从1开始报数。问最后剩下的人的编号。 例如:N = 3,K = 2。2号先出列,然后是1号,最后剩下的是3号。 收起
约瑟夫环模板,递推。
代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define MAX 50000 #define DMAX 10000 using namespace std; typedef long long ll; int n,k,ans = 1; int main() {scanf("%d%d",&n,&k);for(int i = 2;i <= n;i ++) {ans = (ans + k) % i;///ans = (ans + k % i) % i }printf("%d",ans + 1); }
输入
2个数N和K,表示N个人,数到K出列。(2 <= N, K <= 10^6)输出
最后剩下的人的编号输入样例
3 2输出样例
3约瑟夫环模板,递推。
代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define MAX 50000 #define DMAX 10000 using namespace std; typedef long long ll; int n,k,ans = 1; int main() {scanf("%d%d",&n,&k);for(int i = 2;i <= n;i ++) {ans = (ans + k) % i;///ans = (ans + k % i) % i }printf("%d",ans + 1); }
转载于:https://www.cnblogs.com/8023spz/p/10053060.html
总结
以上是生活随笔为你收集整理的51nod 1073 约瑟夫环的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Mac os硬盘空间释放
- 下一篇: ansible基础-playbooks