Last active
January 1, 2016 09:39
-
-
Save FrankWu100/8126349 to your computer and use it in GitHub Desktop.
Linux & Ruby Final exam
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
============================================= | |
第一題 | |
請設計一ruby程式,檔名為exf1.rb,並符合以下所有規範: | |
(1)不加參數時,產生20個1到10的亂數並印出來(一行印出所有亂數),第二行 | |
印出本次出現最多次的數字與其次數(如有同樣個數,印出任一個即可)。 | |
執行範例如下: | |
./exf1.rb | |
4 5 7 1 2 4 7 8 9 1 2 3 7 7 4 7 7 6 6 2 | |
7: 6 times | |
(2) 加一個參數時,參數為個數值,依該傳入參數產生幾個1到10的亂數, | |
./exf1.rb 8 (產生8個亂數) | |
4 6 7 1 2 4 2 8 | |
4: 2 times | |
============================================= | |
第二題 | |
請設計一ruby程式,檔名為exf2.rb,並符合以下所有規範: | |
接收一參數,利用星號印出三角形樣式,如下範例: | |
./exf2.rb 4 | |
* | |
** | |
*** | |
**** | |
./exf2.rb 6 | |
* | |
** | |
*** | |
**** | |
***** | |
****** | |
============================================= | |
第三題 | |
請設計一ruby程式,檔名為exf3.rb,並符合以下所有規範: | |
產生30個1到6的亂數值模擬擲骰,加總各個骰值出現的次數後,依骰值出現的次數由大而小印出長條圖,如下: | |
./exf3.rb | |
4: ********** | |
2: ****** | |
6: ***** | |
5: ***** | |
1: ** | |
3: ** | |
============================================= | |
第四題 | |
請設計一ruby程式,檔名為exf4.rb,並符合以下所有規範: | |
Ticket類別是用來計算捷運票價的類別,共有10個捷運站,分別為A、B、C … J十個站,起站與出站若在4個站(含)內票價為20元,5站到7站為25元,8到10站為30元,請在程式中設計一類別Ticket,並在程式中加入以下程式碼使其執行時能印出規定的結果: | |
tick = Ticket.new(ARGV[0], ARGV[1]) | |
puts tick.price | |
執行結果為: | |
./exf4.rb A F | |
25 (印出A到F站的票價) | |
./exf4.rb B J | |
30 (印出B到J站的票價) | |
============================================= | |
第五題 | |
請設計一ruby程式,檔名為exf5.rb,並符合以下所有規範: | |
Highway類別是用來計算高速公路計程收費,收費標準如下: | |
1) 20公里(含)內為免費,每1公里費率1.2元。 | |
2) 超過200公里的部份,每公里費率為0.9元。(打75折) | |
3) 假設計程偵測全都設在交流道,目前共有六個交流道(A到F),其距離必須在程式中使用Array儲存,距離資料如下: | |
A <15公里> B <47公里> C < 54公里> D <80公里> E <98公里> F | |
請在程式中設計Highway類別,並在程式中加入以下程式碼使其執行時能印出規定的結果: | |
hway=Highway.new | |
puts hway.price(ARGV[0], ARGV[1]) | |
執行結果為: | |
./exf5.rb A D | |
115.2 (15+47+54)*1.2=139.2元,減掉免費20公里的優惠金額24元,得到115.2元 | |
./exf5.rb B F | |
287.1 | |
27+14+80+98=279公里, 279公里*1.2元為334.8元,減掉免費的24元(20公里免費),再減掉超過240元(200公里的費用)部份打7.5折,334.8-24-23.7=287.1元 | |
高速公路計程收費方法可參考: | |
http://www.freeway.gov.tw/Publish.aspx?cnid=1880&p=4289 |
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
#!/usr/bin/ruby | |
if ARGV[0] | |
runTime = ARGV[0].to_i | |
else | |
runTime = 20 | |
end | |
count = Array.new(10,0) | |
runTime.times { | |
r = rand(10)+1 | |
count[r-1] = count[r-1]+1 | |
printf "%d " ,r | |
} | |
printf "\r\n" | |
mostNum=0 | |
for i in 1..10 | |
if (count[i-1] >= count[mostNum]) | |
mostNum=i-1 | |
end | |
end | |
printf "%d: %d times\r\n", mostNum+1, count[mostNum] |
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
#!/usr/bin/ruby | |
size = ARGV[0].to_i | |
for i in 1..size | |
(size-i).times { | |
printf " " | |
} | |
i.times { | |
printf "*" | |
} | |
printf "\r\n" | |
end |
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
#!/usr/bin/ruby | |
count = Array.new(6,0) | |
30.times { | |
r = rand(6)+1 | |
count[r-1] = count[r-1]+1 | |
#printf "%d " ,r | |
} | |
#printf "\r\n" | |
mostNum=1 | |
temp = count | |
6.times { | |
for i in 1..6 | |
if (temp[i-1] >= count[mostNum-1]) | |
mostNum=i | |
end | |
end | |
printf "%d: ", mostNum | |
count[mostNum-1].times { | |
printf "*" | |
} | |
temp[mostNum-1]=0 | |
printf "\r\n" | |
} |
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
#!/usr/bin/ruby | |
class Ticket | |
attr_accessor :price | |
def initialize(s1, s2) | |
@price = countMoney(s1, s2) | |
end | |
def countMoney(s1, s2) | |
rang = findStationRang(s1, s2) | |
if rang <=4 | |
money=20 | |
elsif rang <=7 | |
money=25 | |
elsif rang <=10 | |
money=30 | |
end | |
return money | |
end | |
def findStationRang(s1, s2) | |
stationArray = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] | |
rang = stationArray.index(s1.upcase) - stationArray.index(s2.upcase) | |
if rang < 0 | |
return rang*-1 | |
else | |
return rang | |
end | |
end | |
end | |
tick = Ticket.new(ARGV[0], ARGV[1]) | |
puts tick.price |
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
#!/usr/bin/ruby | |
class Highway | |
def initialize | |
end | |
def price(s1, s2) | |
long = countLong(s1, s2) | |
if long <= 20 | |
return 0 | |
elsif long <= 200 | |
return (long-20)*1.2 | |
else | |
return (200-20)*1.2+(long-200)*0.9 | |
end | |
end | |
def countLong(s1, s2) | |
wayNode = ['A', 'B', 'C', 'D', 'E', 'F'] | |
wayLong = [15, 47, 54, 80, 98] | |
long=0 | |
node1 = wayNode.index(s1.upcase) | |
node2 = wayNode.index(s2.upcase) | |
if node1 > node2 | |
temp = node1 | |
node1 = node2 | |
node2 = temp | |
end | |
for i in node1..(node2-1) | |
long = long + wayLong[i] | |
end | |
#puts long | |
return long | |
end | |
end | |
hway = Highway.new | |
puts hway.price(ARGV[0], ARGV[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment