codeforces-82A-Double Cola( 等比数列 + 有意思的题目! )

A. Double Cola
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The input data consist of a single integer n (1 ≤ n ≤ 109).

It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.

Output

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.

Examples
input
1
output
Sheldon
input
6
output
Sheldon
input
1802
output
Penny
 
         
 
         
 
         
题目的大致意思:就是生活大爆炸的5个主角排队去喝分裂汽水.依次是按照:Sheldon, Leonard, Penny, Rajesh and Howard
的顺序去喝.每个人喝完之后会分裂成两个人,然后这个两个人就从队首依次回到队尾去.周而复始.
比如:
第一次Sheldon喝完之后,队伍的顺序会变成: Leonard , Penny , Rajesh , Howard , Sheldon , Sheldon .
第二次Leonard喝完之后,队伍的顺序会变成: Penny , Rajesh , Howard , Sheldon , Sheldon , Leonard , Leonard.
第三次Penny喝完之后,队伍的顺序会变成: Rajesh , Howard , Sheldon , Sheldon , Leonard , Leonard , Penny , Penny.
...
...
 
         
其实你仔细看一下,其实每次都是以2的倍数增加,而最开始的就只有5个人,这样我们可以把这5个人依次看成,a,b,c,d,e.
然后,每一次分裂对应:
a b c d e , aa bb cc dd ee , aaaa  bbbb cccc dddd eeee , aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ........
看出来没有?好还没看出来的话那:
1 1 1 1 1 , 11 11 11 11 11 , 1111 1111 1111 1111 1111 , 11111111 11111111 11111111 11111111 11111111 .......
这是一个等比数列!问题是让你求第n项的值是多少!(不记得公式?)
          
          
          
          
所以input的值是第n项,而这个等比数列有5组,公比q=2,这样我们便可以列出等式.
即 Sn*5 = n (这里这个n是指输入的input第几个人,下面用input代替,因为有5组,所以必须得乘以5)
(1-2^n)/(1-2)*5 = input
(2^n-1) = input/5
2^n = input/5 + 1
所以只有一个未知数.但是我们这里可能算不出具体的值.因为可能不存在正好为整数的n使得左右两边的等式相等.
接下来的思路就简单了,这样的话我们便算出它的前一组,然后便可以得出input的数在第几组.
用input - (前一组数的前n项和) = 这个数所在组的第几项.
然后用这个数除以它分裂的个数(pow(2,n))取整,
从0开始到4.依次为:Sheldon, Leonard, Penny, Rajesh and Howard
 
         
 
         
//codeforces  82A. Double Cola

#include<iostream>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
    int n,n1 = 0,oldn=0;                             //n:第几个数,n1:所在数的上一组
    int temp;
    cin>>n;                                          //输入input的值
    if(n>5)                                          //如果n大于5才进行运算,因为如果是前5个数直接输出就行.
    {
        oldn = n;                                    //保存一下是第几个人
        n = n/5+1;                                   //公式前n项和推导出:2^n = input/5 + 1
        for(int i=0;i<100;i++)
        {
            if(pow(2,i)>n)                           //如果算的等比数列前n项和比input大,用它上一组的
            {
                n1 = i-1;                            //求上一组的n是多少.
                break;
            }
        }
        //        cout<<n1<<endl;
        //        cout<<oldn<<" "<<(5*(pow(2,n1)-1))<<endl;

        temp = oldn - (5*(pow(2,n1)-1));             //用input减去所在数的上一组的前n项和(别忘了*5)

        //        cout<<temp<<endl;
        //        cout<<(int)(pow(2,n1))<<endl;

        switch (temp/(int)(pow(2,n1))) {           //然后用这个数除以它分裂的个数(pow(2,n1))取整
        case 0:
            cout<<"Sheldon"<<endl;
            break;
        case 1:
            cout<<"Leonard"<<endl;
            break;
        case 2:
            cout<<"Penny"<<endl;
            break;
        case 3:
            cout<<"Rajesh"<<endl;
            break;
        case 4:
            cout<<"Howard"<<endl;
            break;
        }
    }
    else
    {
        switch (n) {                        //因为如果是前5个数直接输出就行.
        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;
        }

    }
    return 0;
}


 
        

你可能感兴趣的:(C++,数学,ACM,等比数列)