题解 | #汽水瓶#

汽水瓶

http://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f


def get_sum(n)
    sum = 0
    sum = n/3.to_i
    @all_sum += sum
    
    # 换完之后的总瓶子数
    n2 = sum + n%3.to_i
    if n2 < 3
        @all_sum += 1 if n2 == 2
        return 
    end
    get_sum(n2)
end

ns = []
while (line = STDIN.gets)
    break if line.to_i == 0
    ns << line.to_i
end

results = []

ns.each{|n|
    @all_sum = 0
    if n <= 4
        @all_sum += (n+1)/3.to_i
    else       
       get_sum(n)
    end
    results << @all_sum
}
puts results


全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务