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 3 years have passed since last update.

AtCoder Beginner Contest 168

Last updated at Posted at 2020-05-18

3回目の挑戦。
##A問題
case文を使うための例題、なのだが全部書き下ろしてパス

abc168a.rb
n = gets.chomp.to_s[-1,1]
#puts n
#puts n[-1,1]
if n=="3"
    puts "bon"
else
    if  n== "0"
    puts "pon"
    else
        if n== "1"
        puts "pon"
        else
            if n== "6"
                puts "pon"
            else
                if n=="8"
                    puts "pon"
                else
                    puts "hon"
                end
            end
        end
    end
end

ちゃんとcase文で書き直し。

case.rb
n = gets.chomp.to_s[-1,1]
case n 
when "3"
   puts "bon"
when "0","1","6","8"
   puts "pon"
else
   puts "hon"
end

##B問題
出力を場合分けするより、長い場合は切り取って末尾に3点ドット付け足した方がスマートですね。

abc168b.rb
k = gets.chomp.to_i
s = gets.chomp.to_s
if s.length <= k
    puts s
else
    print s[0..k-1]
    print "..."
end 

##C問題
2針とも先端の座標が求まるので、2点間の距離を直接計算。
各数値を整数のまま扱ってテストケースの半分AC半分WAとなったので、しばらく考えてfloatに変換してAC.
解説にあったように余弦定理で解くと三角関数3個分計算量減るけど、ループないからこれでも良いかな?

abc168c.rb
imput = gets.chomp.split(" ").map!{|item| item.to_i}
a = imput[0].to_f
b = imput[1].to_f
h = imput[2].to_f
m = imput[3].to_f

hang = Math::PI*(30*h + m/2)/180
mang = Math::PI*m*6/180

hy = Math.sin(hang)*a
hx = Math.cos(hang)*a
my = Math.sin(mang)*b
mx = Math.cos(mang)*b
puts Math.sqrt((hy-my)**2 + (hx-mx)**2)

短針を座標軸とすれば三角関数2つ分の計算は減る。

abc168c.rb
imput = gets.chomp.split(" ").map!{|item| item.to_f}
a = imput[0]
b = imput[1]
h = imput[2]
m = imput[3]

hang = Math::PI*(30*h + m/2)/180
mang = Math::PI*m*6/180
ang = mang - hang

mx = Math.cos(ang)*b
my = Math.sin(ang)*b

puts Math.sqrt((a-mx)**2 + (my)**2)

この辺りで60分超えてD問題読んで解けそうにないので断念。
D/E/Fに挑むにはアルゴリズムの例題解いて身に付けるしかないか。
今回のA問題のように力技依存しないように定番過去問の数こなす必要ありそう。

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?