CodeForces - 82A

本文通过等比数列的方法解决了一个有趣的编程问题——双倍可乐问题。该问题描述了五个人排队购买可乐并分裂的情景,需要找出饮用第n瓶可乐的人是谁。文中提供了一种解决方案,并给出了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Example
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny

题意描述:

五个人排队去喝汽水,依次是:Sheldon, Leonard, Penny, Rajesh and Howard,每个人喝完之后分裂为两人,这两人从队首回到队尾。
第一次Sheldon喝完后: Leonard , Penny , Rajesh , Howard , Sheldon , Sheldon .
第二次Leonard 喝完后:Penny , Rajesh , Howard , Sheldon , Sheldon ,Leonard,Leonard.


相当于一个等比数列,有5组,公比为2。

#include<bits/stdc++.h>
//#include<iostream>
//#include<cstdio>
//#include<cmath>
//#include<iomanip>
using namespace std;
//codeforces 82A.Double Cola
/****等比****/
int main(){
int n,n1=0,n2=0;
int temp;
scanf("%d",&n);
if(n>5){     
    n2=n;
    n=n/5+1;   //前n项和公式 a1(1-q^n) / (1-q)=Sn;(2^n-1)/(2-1)*5=Sn;2^n=Sn/5+1
    for(int i=0;i<100;i++){
        if(pow(2,i)>n){  //等比数列前n项和大于输入数据,使用上一组的前n项和
                n1=i-1; //上一组的n
                break;
        }
    }
    temp=n2-(5*(pow(2,n1)-1));  //input-上一组的前n项和
    switch(temp/(int)(pow(2,n1))){   //用temp/分裂的个数 (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){
     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;
}

后面在博客上发现了其他的骚操作:

#include <string>
#include <iostream>
using namespace std;

int main(){
    string strs[] = {"Sheldon", "Leonard", "Penny", "Rajesh", "Howard"};
    int n = 0;
    cin>>n;

    int i = 1;
    while (n > i*5)//100 - 5 - 10 - 20 - 40 ...
    {
        n -= i*5;
        i <<= 1;
    }
    int a = n / i;
    if (n % i) a++;
    cout<<strs[a-1];
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值