【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[ ...
随机推荐
- shell实现倒计时功能
#!/bin/bash ############################################################## # File Name: oldboyedu.sh ...
- Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights
第一次提交遇到这样的情况,怎么回事呢,我在github上提交了ssh key 的啊. 排查先看看能不能解析, 1.先 ping https://github.com 把ip添加到 host : ...
- 【转】 在Eclipse中使用JUnit4进行单元测试(中级篇)
http://blog.csdn.net/andycpp/article/details/1327346 我们继续对初级篇中的例子进行分析.初级篇中我们使用Eclipse自动生成了一个测试框架,在这篇 ...
- Web里URL空格的转换方法
今天收到一个Bug修复的任务,Bug为在页面上输入一个文件夹名包含空格,点击该文件夹的URL后链接错误. 看URL是HttpUtility.UrlEncode将空格转换成了“+”号,原因找到就着手解决 ...
- Laravel5.1 请求
这篇主要说说Request 每当用户访问我们的网站功能时都会用到它 我们可以使用Request中的一些数据来做出适当的相应. 1 常用的属性和方法 我们可以从Request中取到一些重要属性来做一些逻 ...
- ERROR 1130: Host '' is not allowed to connect to thisMySQL server
今天1网友求助,说自己PHPmyadmin能够正常连接数据库,使用sqlyog报错: ERROR 1130: Host '172.27.214.1' is not allowed to connect ...
- Oracle注入速查表
注:下面的一部分查询只能由admin执行,我会在查询的末尾以"-priv“标注. 探测版本: SELECT banner FROM v$version WHERE banner LIKE ‘ ...
- poj1676(转换为二进制求解)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13175 题目关键:将0~9十个数转换为二进制数进行枚举比较 int ...
- SharePoint让所有用户访问站点
SharePoint让所有用户访问站点,可用在用户组里面添加:NT AUTHORITY\authenticated users
- SharePoint服务器端对象模型 之 访问文件和文件夹(Part 2)
4.添加文件夹 文件夹的创建方法在文档库和普通列表中稍有不同. 在文档库中,与一般的集合操作相同,直接使用SPFolderCollection的Add(string name)方法即可添加文件夹,例如 ...