[RUBY] AtCoder Beginner Contest 168

The third challenge.

A problem

An example for using a case statement, but write down everything and pass

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

Rewrote it properly with a case statement.

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 problem

If the output is long, it is smarter to cut it out and add 3 dots at the end, rather than dividing the output into cases.

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 problem

Since the coordinates of the tip of both needles can be obtained, the distance between the two points can be calculated directly. Since each numerical value was treated as an integer and became half AC and half WA of the test case, I thought about it for a while and converted it to float and AC. As mentioned in the explanation, solving with the cosine theorem reduces the amount of calculation by three trigonometric functions, but is this okay because there is no loop?

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)

If the short hand is used as the coordinate axis, the calculation for two trigonometric functions will be reduced.

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)

I gave up because I couldn't solve the D problem after reading the D problem for more than 60 minutes around here. The only way to challenge D / E / F is to solve the algorithm example and acquire it. It seems that it is necessary to handle a number of standard past questions so as not to depend on power skills like the A problem this time.

Recommended Posts

AtCoder Beginner Contest 168
AtCoder Beginner Contest 182 Participation Article
AtCoder Beginner Contest 132 D Problem
Solve AtCoder Beginner Contest 151 in java
Solve AtCoder Beginner Contest 153 in java
Solve AtCoder Beginner Contest 160 in java
Solve AtCoder Beginner Contest 152 in java
Solve AtCoder Beginner Contest 156 in java
AtCoder Beginner Contest 167 C Problem (Java)
AtCoder Beginner Contest 169 A, B, C with ruby
AtCoder Beginner Contest 170 A, B, C up to ruby
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Solving with Ruby AtCoder ACL Beginner Contest C Union Find (DSU)
java beginner 4
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
java beginner
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)