Last active
December 6, 2016 14:44
-
-
Save s890081tonyhsu/07b04a3b258687e090c6a2c2d898183d to your computer and use it in GitHub Desktop.
https://paiza.jp/moshijo/ B級以上卡關紀錄
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 利用陣列的push語pop實作路徑跳躍 | |
c, p = gets.chomp.split(" ") # 因為兩個路徑寫在一起,要切開 | |
c = c.split("/") | |
p = p.split("/") | |
c.delete_at(0) if c.length != 0 # 根目錄在split的時候項目為0,所以強制非0的第一項要剃除 | |
p.each do |path| | |
if path == ".." # 向上跳 | |
if c.length == 0 # 但是根目錄不能往上跳,不然ruby會傲嬌 | |
nil | |
else | |
c.pop | |
end | |
elsif path == "." # 當前目錄,不跳 | |
nil | |
else | |
c.push(path) | |
end | |
end | |
puts "/" + c.join("/") # 組合回目錄的時候<記得把根目錄附上 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 這題主要是說將每一個四捨五入的組合找出最大值,但是用數字的觀點出發太困難惹 | |
# 所以就把數字變成字串陣列來處理 | |
a = gets.chomp.split("").map(&:to_i) # 讀入數值先做切割轉換,順便取得長度 | |
l = a.length | |
if a[0] > 4 # 如果四捨五入最高位數就可以進位,那最大值就簡單了 | |
puts 10.to_s + '0' * (l-1) | |
else # 不然還是辛苦一點一位一位測,反正最大的一定是從個位數一個一個四捨五入上來的 | |
(l-1).downto(1) do |i| | |
if a[i] > 4 | |
a[i] = 0 | |
a[i-1] += 1 | |
end | |
end | |
puts a.map(&:to_s).join("") # 用陣列四捨五入好之後記得併回去 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment