/* 题目: 约瑟夫环问题。 思路: 数学规律 f(n)=0(n=1),[f(n-1,m)+m]%n(n>1) */ #include<iostream> #include<list> using namespace std; int LastRemaining(unsigned int n,unsigned int m){ if(n < 1|| m < 1) return -1; if(n == 1) return 0; int last = 0; for(int i = 2; i <= n; i++){ last = (last + m) % i; } return last; } int main(){ cout<<LastRemaining(5,3); }