Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@nidouchi

# 第19回オフラインリアルタイムどう書くの参考問題をRubyで解く

``````def solve(q)
nums = q.split(',').map(&:to_i)
max_num = nums.max
sums = []
(1..max_num).each {|x|
(x..max_num).each {|y|
(y..max_num).each {|z|
sum = [x,y,z,x*2,y*2,z*2,x*3,y*3,z*3]
[x,y,z].combination(2){|a,b|
sum << a + b
}
[x,x,y,y,z,z].combination(3){|a,b,c|
sum << a + b + c
}
if ((nums & sum) == nums)
sums << [x,y,z]
end
}
}
}
case sums.size
when 0
'none'
when 1
sums[0].join(',')
else
'many'
end
end

puts Time.now
no,q,a = line.chop.split(/\s+/)
ans = solve(q)
print no + "\t" + ans
puts ans == a ? ' o' : ' x'
end
puts Time.now

__END__
0   3,11,12,102,111,120 1,10,100
1   10,20,30,35,70  many
2   1,5,20,80   none
3   1,2,3,4,5,6,7,8,9,10,11,12,13,14    many
4   1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 1,4,5
``````
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?