0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

蟻本の三角形をrubyで解いてみる(記録用)

Last updated at Posted at 2019-07-25

問題

n本の棒があり、棒iの長さはai。それらの棒から3本を選んでできるだけ周長の長い三角形を作ろうと考えている。最大の周囲はいくら?ただし、三角形が作れない場合は、0と答える。

入力例

5
2 3 4 5 10

出力例

12(3,4,5と選んだとき)

解答

a=gets.to_i
b=gets.chomp.split.map(&:to_i)
ans=0
b.permutation(3){|x|
  max=x.max
  total=x.inject(:+)
  rest=total-max
  if max<rest
    if total > ans
      ans = total
    end  
  end  
}
puts ans
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?