CodeForces-82A-Double Cola

来源:互联网 发布:windows私有云 编辑:程序博客网 时间:2024/04/26 03:40
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.

Sample test(s)
input
1
output
Sheldon
input
6
output
Sheldon
input
1802
output
Penny
import java.util.*;public class DoubleCola {public static void main(String[] args) {Scanner inScanner = new Scanner(System.in);int n = inScanner.nextInt();List<Integer> list = new ArrayList<Integer>();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);// loop n-1 timewhile (n-- > 1) {// ini updatelist.add(list.get(0));list.add(list.get(0));list.remove(0);}switch (list.get(0)) {case 1:System.out.println("Sheldon");break;case 2:System.out.println("Leonard");break;case 3:System.out.println("Penny");break;case 4:System.out.println("Rajesh");break;case 5:System.out.println("Howard");}}}
思考:用这种方法较为直观的通过数组展现了整个题目的过程,但是没有经过算法的优化,所以无法通过AC,会出现时间超限。
改进的算法为
int n=sc.nextInt()-1;while (n>=5)    n=(n-5)/2;



0 0
原创粉丝点击