原题链接:https://codeforces.com/problemset/problem/82/A
题意:有一个队列初始五个人,后来出队的人翻倍增长排在队尾,那么当第一队排完之后第二对就10个人了,然后一直这样无止境的排下去,问第n个人叫什么?
解题思路:首先我们要确定在哪个组,在这确定过程中我们要更新n的值,以及获取第几组的信息。再判断这个组内的更新后的值n是谁?具体看代码。
AC代码:
/*
*/
//低版本G++编译器不支持,若使用这种G++编译器此段应注释掉
//i为循环变量,a为初始值,n为界限值,递增
//i为循环变量, a为初始值,n为界限值,递减。
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为代码自定义代码模板***************************************//
string str[5]={"Sheldon","Leonard","Penny","Rajesh", "Howard"};
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
ios::sync_with_stdio(false);//打消iostream中输入输出缓存,节省时间。
cin.tie(0); cout.tie(0);//可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
int n;
int cnt;//统计在第几组。
while(cin>>n){
cnt=0;
while(n>5*pow(2,cnt)){
//cnt记录在第几组,n为更新后的值。
n-=5*pow(2,cnt);
cnt++;
}
int temp=(n-1)/(pow(2,cnt));//pow(2,cnt)获取在这个队列中每个名字出现的次数。
cout<<str[temp]<<endl;
}
return 0;
}