Last active
August 29, 2015 14:27
-
-
Save paulosuzart/b9db21997364bf18b06c to your computer and use it in GitHub Desktop.
Sherlock and The Beast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Enter your code here. Read input from STDIN. Print output to STDOUT | |
| def br = new BufferedReader(new InputStreamReader(System.in)) | |
| def lineAsInt(br) { | |
| return Integer.parseInt(br.readLine()) | |
| } | |
| def MAX_T = 20 | |
| def readT(br) { | |
| def t = lineAsInt(br) | |
| if (t > 20) { | |
| println(-1) | |
| System.exit(1) | |
| } | |
| return t | |
| } | |
| def maxT = readT(br) | |
| def run(max) { | |
| def fives = max - (max % 3) | |
| def threes = max - (max % 5) | |
| def out = "" | |
| while (out == "") { | |
| if (fives == max) { | |
| (1..fives).each {out = out + "5"} | |
| break | |
| } | |
| if (threes == max) { | |
| (1..threes).each {out = out + "3"} | |
| break | |
| } | |
| if (fives + threes > max) { | |
| fives = fives - 3 | |
| threes = threes - 5 | |
| } else if (fives + threes == max) { | |
| if (fives > 0) { | |
| (1..fives).each {out = out + "5"} | |
| } | |
| if (threes > 0) { | |
| (1..threes).each {out = out + "3"} | |
| } | |
| break | |
| } else { | |
| out = "-1" | |
| break | |
| } | |
| } | |
| println(out) | |
| } | |
| for (i in 1..maxT) { | |
| run(lineAsInt(br)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment