码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - Sherlock and The Beast

时间:2015-03-02 12:44:57      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

Greedy beats DP this time...

I tried several DP solutions first, but all failed with RE\TLE. If you ‘feel‘ the problem, Greedy should be working:

(A solution from discussion)

def getPivot(n):
    while n > 0:
        if n % 3 == 0:
            break;
        else:
            n -= 5
    return n
        
T = input()
for i in xrange(T):
    N = int(input())
    pivot = getPivot(N)
    if pivot < 0:
        print -1
    else:
        str = ‘‘
        repeat = pivot / 3
        while repeat > 0:
            str += 555
            repeat -= 1
        repeat = (N - pivot) / 5
        while repeat > 0:
            str += 33333
            repeat -= 1
        print str

HackerRank - Sherlock and The Beast

标签:

原文地址:http://www.cnblogs.com/tonix/p/4308337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!