【HackerRank】 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的更多相关文章
- 【HackerRank】Sherlock and MiniMax
题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer ...
- 【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 ...
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【hackerrank】Week of Code 30
Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...
- 【hackerrank】Week of Code 26
在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- 【HackerRank】Coin on the Table
题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...
- 【HackerRank】Pairs
题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...
随机推荐
- struts-config.xml 文件:
struts-config.xml配置文件是一个在Web客户端组件的视图和模型之间的联系,但你的项目的99.99就不会碰这些设置%.基本的配置文件包含以下主要内容: SN Interceptor &a ...
- Eclipse 任务管理
管理任务 在Eclipse中用TODO标签管理任务,利用这个功能可以方便地将项目中一些需要处理的任务记录下来. 我们可以在 Java 代码中的注释添加 TODO 单词来标记一个任务,任务可以通过 Ta ...
- selenium + js 处理窗口
1.隐藏页面的广告窗口 document.getElementById("top_left").style.display="none"; 2.隐藏控件点击 d ...
- js创建对象的最佳方式
1.对象的定义 ECMAScript中,对象是一个无序属性集,这里的“属性”可以是基本值.对象或者函数 2.数据属性与访问器属性 数据属性即有值的属性,可以设置属性只读.不可删除.不可枚举等等 访问器 ...
- 学习php必须要了解的一些知识
前言:每个人的成功都是用辛勤的劳动换来的 一.网络的基础知识 IP地址:Internet protocol address 指的是互联网协议地址,由二进制构成,(IPV4是32位的二进制),我们人为的 ...
- 守护进程监控tomcat并自己主动重新启动
昨天的tomcat问题.一天挂了3,4回,受不了了决定写个监控tomcat进程并自己主动重新启动的脚本! 在网上查资料.主要分为两类:一类是定时重新启动tomcat,这当然不是我须要的.还有一类是监控 ...
- shell 命令getopts用法
写shell脚本常见sh test.sh -m 2 -d 3的写法 事例脚本: #!/bin/bash while getopts ":a:b:c:" arg #选项后面的冒号表示 ...
- 查看java进程的所有信息
查看java 进程下的所有信息 ll /proc/pid/fd ru:ll /proc/24047/fd
- 打日志--以python为例
日志报错要去修,要不然是隐患,总有一天会爆炸 增加日志是排错的好方法,不要不舍得加日志,比如怕代码变难看,怕日志输出太多. python logging exc_info sys.exc_info() ...
- 一个经验丰富的网站建设程序员的CSS资料
没有就不能活的 53 个 CSS 技术 对新手实用的 20 个 CSS 建议 快速编写更好 CSS 代码的 5 种方法 50+ 个 CSS 创意案例和教程 101 个 CSS 小贴士.教程和范例 CS ...