这个题是一个约瑟夫问题,然而是要找规律的:
设剩下人数为n,
若n是偶数,则一轮过后只剩下奇数位的人,有n = n / 2,原本在奇数位的数字变成(k+1) / 2;
若n是奇数,则一轮过后只剩下奇数位的人,特别的原本为第一位的也应被删除,原本第3位的变成第一位,于是有n = (n-1) / 2,原本在奇数位的数字变成(k-1) / 2;
经过有限次数后,n一定变成1,这就是最后的save。
因此逆推上去就知道save开始所处位置了。
View Code
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<map> #include<cstring> #include<vector> #include<string> #define LL long long using namespace std; int f( int n ) { if( n == 1 ) return 1; if( n & 1 ) return f( ( n -1 )/2 )*2 + 1; else return f( n/2 )*2 - 1; } int main( ) { int d,p; while( scanf( "%de%d",&d,&p ),d|p ) { while( p -- ) d *= 10; printf( "%d\n",f( d ) ); } //system( "pause" ); return 0; }