CodeForces - 82A-day3-B

问题来源

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.

例子

在这里插入图片描述

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语句判断。
后面的按等比数列公式推导,先用给出来的序号减去前面的行的人数总数。
在这里插入图片描述
得到所求的人在自己的行的序号,每一行都有2^n*5个人(n为行序号)序号除以该行取余得到0~4分别对应那五个人
再用一次switch即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值