【HackerRank】 Sherlock and The Beast

Sherlock and The Beast

Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has been facing weird problems with their supercomputer, ‘The Beast‘, recently.

This afternoon, Sherlock received a note from Moriarty, saying that he
has infected ‘The Beast‘ with a virus. Moreover, the note had the number
N printed on it. After doing some calculations, Sherlock
figured out that the key to remove the virus is the largest ‘Decent‘
Number having N digits.

A ‘Decent‘ Number has -
1. Only 3 and 5 as its digits.
2. Number of times 3 appears is divisible by 5.
3. Number of times 5 appears is divisible by 3.

Meanwhile, the counter to destruction of ‘The Beast‘ is running very
fast. Can you save ‘The Beast‘, and find the key before Sherlock?

Input Format
The 1st line will contain an integer T, the number of test cases, followed by T lines, each line containing an integer N i.e. the number of digits in the number

Output Format
Largest Decent number having N digits. If no such number exists, tell Sherlock that he is wrong and print ‘-1‘

Constraints
1<=T<=20
1<=N<=100000



题解:

Java的String真是坑死爹啊,每次加入一个新的字符都要用O(N)的时间,所以如果不停的加入字符要用StringBuffer或者StringBuilder,要不就一直TLE。

代码如下:

 1 import java.io.*;
 2 import java.util.*;
 3
 4
 5 public class Solution {
 6
 7     public static void main(String[] args) {
 8         Scanner in = new Scanner(System.in);
 9         int t = in.nextInt();
10         for(int i =0;i<t;i++){
11             int n = in.nextInt();
12             StringBuffer answer = new StringBuffer();
13             for(int j = 0;j <= n/5;j++){
14                 if((n-5*j)%3 == 0){
15                     for(int k = 0;k< n-5*j;k++)
16                         answer.append("5");
17                     for(int k = 0;k < 5*j;k++)
18                         answer.append("3");
19                     System.out.println(answer.toString());
20                     break;
21                 }
22             }
23             if(answer.toString().equals(""))
24                 System.out.println(-1);
25         }
26     }
27
28
29
30 }

【HackerRank】 Sherlock and The Beast

时间: 2024-10-10 12:01:42

【HackerRank】 Sherlock and The Beast的相关文章

【HackerRank】Sherlock and MiniMax

题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For

【HackerRank】Sherlock and Array

Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right. If there are no elements to left/right, then sum

【HackerRank】Median

题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大堆存放前半部分较小的元素,最小堆存放后半部分较大的元素,并且最大堆的所有元素小于最小堆的所有元素:保持最大堆最多比最小堆多一个元素.每次插入元素的时候都先插入到最大堆,如果发现最大堆比最小堆多了两个个,那么就从最大堆里面拿出最大的放到最小堆里面:如果发现最大堆里面新插入的元素破坏了最大堆所有元素小于

【HackerRank】Game Of Rotation

题目连接:Game Of Rotation Mark is an undergraduate student and he is interested in rotation. A conveyor belt competition is going on in the town which Mark wants to win. In the competition, there's A conveyor belt which can be represented as a strip of 1

【HackerRank】Bus Station

有n组好朋友在公交车站前排队.第i组有ai个人.还有一辆公交车在路线上行驶.公交车的容量大小为x,即它可以同时运载x个人. 当车站来车时(车总是空载过来),一些组从会队头开始走向公交车. 当然,同一组的朋友不想分开,所以仅当公交车能容纳下整个组的时候,他们才会上车.另外,每个人不想失去自己的位置,即组的顺序不会改变. 问题时如何选择公交车的容量大小x使得它可以运走所有组的人,并且公交车每次从车站出发时没有空位?(在车里的总人数恰好达到x)? 输入格式 第一行只包含一个整数n.第二行包含n个空格分

【HackerRank】 The Full Counting Sort

In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the

【HackerRank】Manasa and Stones

Change language : Manasa 和 她的朋友出去徒步旅行.她发现一条小河里边顺序排列着带有数值的石头.她开始沿河而走,发现相邻两个石头上的数值增加 a 或者 b. 这条小河的尽头有一个宝藏,如果Manasa能够猜出来最后一颗石头上的数值,那么宝藏就是她的.假设第一个石头的上数值为0,找出最后一个石头的可能的所有数值. 输入格式 第一行包含整数 T, 代表测试数据的组数. 每组数组包含三行: 第一行包含 n,代表石头的个数 第二行包含 a 第三行包含 b 输出格式 升序输出最后一

【HackerRank】QuickSort(稳定快排,空间复杂度O(n))

QuickSort In the previous challenge, you wrote a partition method to split an array into 2 sub-arrays, one containing smaller elements and one containing larger elements. This means you 'sorted' half the array with respect to the other half. Can you

【HackerRank】 Chocolate Feast

Little Bob loves chocolates, and goes to the store with $N money in his pocket. The price of each chocolate is $C. The store offers a discount: for every M wrappers he gives the store, he'll get one chocolate for free. How many chocolates does Bob ge