问题来源

CodeForces - 82A

问题描述

Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a “Double Cola” drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.

For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.

Write a program that will print the name of a man who will drink the n-th can.

Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.

输入

Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a “Double Cola” drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.

For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.

Write a program that will print the name of a man who will drink the n-th can.

Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.

输出

Print the single line — the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: “Sheldon”, “Leonard”, “Penny”, “Rajesh”, “Howard” (without the quotes). In that order precisely the friends are in the queue initially.

例子

CodeForces - 82A-day3-B

AC的代码

#include<string>
#include<math.h>

#include <math.h>
#include<cmath>
#include<algorithm>
#include <iostream>
using namespace std;
int main()
{
    int a;
    cin >> a;
    if(a<=5)
        switch (a)
        {
        case 1:cout << "Sheldon" << endl; break;
        case 2:cout << "Leonard" << endl; break;
        case 3:cout << "Penny" << endl; break;
        case 4:cout << "Rajesh" << endl; break;
        case 5:cout << "Howard" << endl; break;

        }
    else
    {

        int s = (double)log((double)((a + 5) / 5)) / log((double)2);
            int c = 5 * (int)(pow((double)2, (double)s) - 1);
        int g = a-c;
        //if (pow((double)2, (double)(s)) - (double)(int)pow((double)2, (double)(s)) < 0.0000000001)
        {
            //int v = ((a - c) / (int)pow((double)2, (double)(s)))-1;
        }
        int v = (a-c)/ (int)pow((double)2, (double)(s));
        switch (v+1)
        {
        case 1:cout << "Sheldon" << endl; break;
        case 2:cout << "Leonard" << endl; break;
        case 3:cout << "Penny" << endl  ;   break;
        case 4:cout << "Rajesh" << endl ;  break;
        case 5:cout << "Howard" << endl ;  break;

        }
    }

}

解题思路

前五个的顺序是一定的 switch语句判断。
后面的按等比数列公式推导,先用给出来的序号减去前面的行的人数总数。
CodeForces - 82A-day3-B
得到所求的人在自己的行的序号,每一行都有2^n*5个人(n为行序号)序号除以该行取余得到0~4分别对应那五个人
再用一次switch即可。

相关文章: