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。

代码如下:

 import java.io.*;
import java.util.*; public class Solution { public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i =0;i<t;i++){
int n = in.nextInt();
StringBuffer answer = new StringBuffer();
for(int j = 0;j <= n/5;j++){
if((n-5*j)%3 == 0){
for(int k = 0;k< n-5*j;k++)
answer.append("5");
for(int k = 0;k < 5*j;k++)
answer.append("3");
System.out.println(answer.toString());
break;
}
}
if(answer.toString().equals(""))
System.out.println(-1);
}
} }

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

  1. 【HackerRank】Sherlock and MiniMax

    题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer  ...

  2. 【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 ...

  3. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  4. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  5. 【hackerrank】Week of Code 30

    Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...

  6. 【hackerrank】Week of Code 26

    在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...

  7. 【HackerRank】Median

    题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...

  8. 【HackerRank】Coin on the Table

    题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...

  9. 【HackerRank】Pairs

    题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...

随机推荐

  1. 给linux操作系统安装中文环境

    如果设置的默认环境是英文,需要安装中文环境.最简答的方法如下: sudo apt-get install language-pack-zh-hant sudo apt-get install lang ...

  2. Eclipse Debug 调试

    Debug 调试 Java 程序 我们可以在 Package Explorer 视图调试 Java 程序,操作步骤如下: 鼠标右击包含 main 函数的 java 类 选择 Debug As > ...

  3. js 数组容易弄混的那些方法

    js数组中 我们很多数组的方法都知道其中的含义 但是每一次用的都不是很顺手 下边我给大家写了一个小小的demo 来理解那些 近似”双胞胎“的东西 // splice方法 var a=[1,2,3,4, ...

  4. go反射----1类型

    声明:文章内容取自雨痕老师<Go语言学习笔记> 反射( reflect )让我们能在运行期探知对象的类型信息和内存结构,这从一定程度上弥补了静态语言在动态行为上的不足.同时,反射还是元编程 ...

  5. Ubuntu 14.04 Vim编辑文件的一般操作

    vim编辑文件的一般操作 1. vim #在命令行中输入vim,进入vim编辑器 2. i #按一下i键,下端显示 --INSERT-- #插入命令,在vim中可能任意字符都有作用 3. Esc #退 ...

  6. jmeter对响应结果做正则、json、xpath结果测试

    上面的返回结果可用于关联取值测试 具体用法详见http://www.cnblogs.com/xinjing-jingxin/p/8554338.html http://goessner.net/art ...

  7. 【BZOJ3444】最后的晚餐 乱搞

    [BZOJ3444]最后的晚餐 Description [问题背景] 高三的学长们就要离开学校,各奔东西了.某班n人在举行最后的离别晚餐时,饭店老板觉得十分纠结.因为有m名学生偷偷找他,要求和自己暗恋 ...

  8. 学习 Tornado

    异步编程 预习 lambda Lambda functions can be used wherever function objects are required. They are syntact ...

  9. C#设计模式-单实例

    单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 1.经典的模式 namespace singleClass { class ...

  10. Java内存模型之happens-before

    转载自    http://www.cnblogs.com/chenssy/p/6393321.html 无论处理器.JVM.编译器都会都保证程序正确的前提下尽可能的对指令执行效率进行优化,进行指令重 ...