Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 01-hello.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

# ...

puts "(请替换成最后的答案)"
puts "Hi, #{your_name}"
2 changes: 1 addition & 1 deletion 02-variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
puts "a 是 #{a}"
puts "b 是 #{b}"

# ...
a, b = b, a

puts "a 应该是 2,现在是 #{a}"
puts "b 应该是 1,现在是 #{b}"
Expand Down
4 changes: 2 additions & 2 deletions 03-triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
print "请输入直角三角形的底边,然后按 Enter: "
b = gets

# .....
c = (a.to_i) * (b.to_i) / 2

puts "直角三角形的面积是: _________"
puts "直角三角形的面积是: #{c}"
8 changes: 5 additions & 3 deletions 04-pizzas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
print "请输入有多少人要吃,然后按 Enter: "
people = gets

# .....
a = (pizzas.to_i) / (people.to_i)

puts "每人可分得几片: _________ 片"
puts "还剩下几片: _________ 片"
b = (pizzas.to_i) % (people.to_i)

puts "每人可分得几片: #{a} 片"
puts "还剩下几片: #{b} 片"
15 changes: 12 additions & 3 deletions 05-bmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@
print "请输入您的身高(厘米),然后按 Enter: "
height = gets

# .....
w = weight.to_f
h = height.to_f / 100
bmi = (w / (h * h)).round(2)

puts "您的 BMI 是: _________"
if bmi < 18.5
bmi_result = "過輕"
elsif bmi >= 24
bmi_result = "過重"
else bmi >=18.5 || bim <=24
bmi_result = "正常"
end

puts "您的 BMI 结果是: _________(过轻或正常或过重)"
puts "您的 BMI 是: #{bmi}"
puts "您的 BMI 结果是: #{bmi_result}"
18 changes: 15 additions & 3 deletions 06-interger-positive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@
print "请输入一个整数,然后按 Enter: "
x = gets

# ....
if x.to_i > 0
inter_result = "正數"
elsif x.to_i == 0
inter_result = "零"
else x.to_i <0
inter_result = "負數"
end

puts "这个数是_____ (正数或零或负数)"
puts "这个数是_____ (偶数或奇数)"
if x.to_i % 2 == 0
this_number = "偶數"
else
this_number = "奇數"
end

puts "这个数是#{inter_result} (正数或零或负数)"
puts "这个数是#{this_number} (偶数或奇数)"
21 changes: 19 additions & 2 deletions 07-abcde.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
print "请输入一个整数z,然后按 Enter: "
z = gets

# ....
if x.to_i < 0
result = "A"
else
if y.to_i > 0
if z.to_i > 0
result = "B"
else
result = "C"
end
else
if z.to_i > 0
result = "D"
else
result = "E"
end
end
end

puts "结果是________(A或B或C或D或E)"

puts "结果是#{result}(A或B或C或D或E)"
16 changes: 14 additions & 2 deletions 08-find-max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
print "请输入一个数字z,然后按 Enter: "
z = gets

# ....
if x > y
if x > z
find_max = "x"
else
find_max = "z"
end
else
if y > z
find_max = "y"
else
find_max = "z"
end
end

puts "最大的数是 ________(x或y或z)"
puts "最大的数是 #{find_max}(x或y或z)"
4 changes: 2 additions & 2 deletions 09-function.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 题目: 输入直角三角形的宽和高,输出三角形的面积

def calculate_area(a, b)
# ....
(b.to_i * a.to_i) / 2
end

print "请输入直角三角形的高,然后按 Enter: "
Expand All @@ -12,4 +12,4 @@ def calculate_area(a, b)

answer = calculate_area(a,b)

puts "直角三角形的面积是: #{answer}"
puts "直角三角形的面积是: #{answer}"
15 changes: 14 additions & 1 deletion 10-function.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# 题目: 使用者输入 x,y,z,请输出三个数中最大的数

def find_max(x, y, z)
if x > y
if x > z
"x"
else
"z"
end
else
if y > z
"y"
else
"z"
end
end
end

print "请输入一个数字x,然后按 Enter: "
Expand All @@ -16,4 +29,4 @@ def find_max(x, y, z)

answer = find_max(x,y,z)

puts "最大的数是 #{answer}"
puts "最大的数是 #{answer}"
8 changes: 4 additions & 4 deletions 11-seven.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

i = 1
while ( i <= 100 )

# ....

if i % 7 == 0
puts i
end
i+=1
end
end
7 changes: 4 additions & 3 deletions 12-sum-even.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

while ( i <= 100 )

# ....

if i % 2 == 0
total += 1
end
i+=1
end

puts total
puts total
25 changes: 22 additions & 3 deletions 13-nn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,28 @@
print "请输入数字 N,然后按 Enter: "
n = gets

# while ( ... )
# while ( ...)
#
# i = 1
# while ( i <= n.to_i )
# j = 1
# while ( j <= i)
# result = i * j
# print "#{j} x #{i} = #{result} "
# j += 1
# end
#
# print"\n"
# i += 1
# end

i = 0
j = 0
n = n.to_i
while (i <= n )
while (j <= n)
result = i * j
puts "#{i} x #{j} = #{result}"
j+=1
end
i+=1
j = 0
end
21 changes: 14 additions & 7 deletions 14-prime.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# 输入一个数字 N,请检查是不是质数

def is_prime(n)
# ....
def is_prime(num)
for i in 2..n/2
if n % i == 0
false
else
ture
end
end
end


print "请输入数字 N,然后按 Enter: "
n = gets

if is_prime(n.to_i)
puts "这是质数"
else
puts "这不是质数"
end
# if is_prime(n.to_i)
# puts "这是质数"
# else
# puts "这不是质数"
# end
7 changes: 5 additions & 2 deletions 15-guess-number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
if n.to_i == target
puts "恭喜猜中啦! "
break
elsif n.to_i > target
puts "太高了,再猜一次"
else
puts "太低了,再猜一次"
end

end
end
2 changes: 1 addition & 1 deletion 16-array-sum.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 给定一阵列内含数字,输出最大值

def find_max(array)
#....
array.max
end

arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88]
Expand Down
13 changes: 9 additions & 4 deletions 17-array-stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

puts arr.to_s

puts "总和是 _____"
puts "平均是 _____"
puts "最大值是 _____"
puts "最小值是 _____"
sum = 0
arr.each do
|a| sum += a
end

puts "总和是#{sum.to_s }"
puts "平均是 #{sum.to_f/arr.length}"
puts "最大值是 #{arr.max}"
puts "最小值是 #{arr.min}"
6 changes: 4 additions & 2 deletions 18-square.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
print "请输入数字 N,然后按 Enter: "
n = gets

# ...
for i in 0..n.to_i
arr[i] = i * i
end

puts arr.to_s
puts arr.to_s
10 changes: 8 additions & 2 deletions 19-filter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# 给定一阵列内含数字,输出另一个数组只包含偶数

def filter_even(arr)
#...
result = []
arr.each do |a|
if a % 2 == 0
result << a
end
end
return result
end

arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1]

puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86]
puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86]
10 changes: 8 additions & 2 deletions 20-sorting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
# Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复

def filter_even(arr)
#...
arry_result = []
arr.each do |result|
if result % 2 == 0
arry_result << result
end
end
return arry_result
end

arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1]


puts "________" # 应该是 [42, 46, 68, 86]
puts filter_even(arr).sort.uniq.to_s # 应该是 [42, 46, 68, 86]
13 changes: 11 additions & 2 deletions 21-selection-sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
# https://zh.wikipedia.org/wiki/选择排序

def selection_sort(arr)
#...
(0...arr.length).each do |j|
key = arr[j]
i = j -1
while i >= 0 && arr[i] > key
arr[i + 1] = arr[i]
i = i - 1
end
arr[i + 1] = key
end
arr
end

arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1]

answer = selection_sort(arr)

puts answer.to_s # 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91]
puts answer.to_s # 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91]
7 changes: 6 additions & 1 deletion 22-missing.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# 给定一阵列内含数字,请输出 0~9 中不见的数字

def find_missing(arr)
# ...
result = []
for i in 0..9
result << i
end
missing_number = result - arr
return missing_number
end

answer = find_missing( [2,2,1,5,8,4] )
Expand Down
Loading