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 "Hello, #{your_name}"
5 changes: 4 additions & 1 deletion 02-variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
puts "a 是 #{a}"
puts "b 是 #{b}"

a = a + b
b = a - b
a = a - b

# ...

puts "a 应该是 2,现在是 #{a}"
puts "b 应该是 1,现在是 #{b}"

3 changes: 2 additions & 1 deletion 03-triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
b = gets

# .....
area = 0.5 * a.to_f * b.to_f

puts "直角三角形的面积是: _________"
puts "直角三角形的面积是: #{area}"
6 changes: 4 additions & 2 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
b = (pizzas.to_i - a) / people.to_i
# .....

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

bmi = weight.to_f / (height.to_f / 100)**2

if bmi < 18.5
result = '过轻'
elsif bmi < 24.0
result = '正常'
else
result = '过重'
end

# .....

puts "您的 BMI 是: _________"
puts "您的 BMI 是: #{bmi}"

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

if x.to_i > 0
result1 = '正数'
elsif x.to_i < 0
result1 = '负数'
else
result1 = '零'
end

if x.to_f % 2 ==0
result2 = '偶数'
else
result2 = '奇数'
end

# ....

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

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

# ....

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

if x.to_f >= y.to_f
if x.to_f >= z.to_f
result = x
else
result = z
end
else
if y.to_f >= z.to_f
result = y
else
result = z
end
end
# ....

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

def calculate_area(a, b)
# ....
area = 0.5 * a.to_f * b.to_f
return area
end

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

answer = calculate_area(a,b)

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

def find_max(x, y, z)
if x.to_f >= y.to_f
if x.to_f >= z.to_f
result = x
else
result = z
end
else
if y.to_f >= z.to_f
result = y
else
result = z
end
end
return result
end

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

answer = find_max(x,y,z)

puts "最大的数是 #{answer}"
puts "最大的数是 #{answer}"
8 changes: 5 additions & 3 deletions 11-seven.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
i = 1
while ( i <= 100 )

# ....

if i % 7 == 0
puts i
end

i+=1
end
end
6 changes: 4 additions & 2 deletions 12-sum-even.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

while ( i <= 100 )

# ....
if i % 2 ==0
total += i
end

i+=1
end

puts total
puts total
18 changes: 11 additions & 7 deletions 13-nn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

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

# while ( ... )
# while ( ...)
#
# end
# end

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
11 changes: 10 additions & 1 deletion 14-prime.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# 输入一个数字 N,请检查是不是质数

def is_prime(n)
# ....
i = 2
result = true
while i < n.to_i
if n.to_i % i == 0
result = false
break
end
i +=1
end
return result
end

print "请输入数字 N,然后按 Enter: "
Expand Down
11 changes: 7 additions & 4 deletions 15-guess-number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

#puts "太低了,再猜一次"
#puts "太高了,再猜一次"

if n.to_i == target
puts "恭喜猜中啦! "
if n.to_i < target
puts "太低了,再猜一次"
elsif n.to_i > target
puts "太高了,再猜一次"
elsif n.to_i == target
puts "恭喜猜中啦!"
break
end

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

def find_max(array)
#....
result = array.max
return result
end

arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88]

max = find_max(arr)
puts "Max is #{max}" # 应该是 88

13 changes: 9 additions & 4 deletions 17-array-stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
end
end

suma = arr.inject(0, :+)
avga = suma.to_f / arr.size
maxa = arr.max
mina = arr.min

puts arr.to_s

puts "总和是 _____"
puts "平均是 _____"
puts "最大值是 _____"
puts "最小值是 _____"
puts "总和是 #{suma}"
puts "平均是 #{avga}"
puts "最大值是 #{maxa}"
puts "最小值是 #{mina}"
8 changes: 6 additions & 2 deletions 18-square.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
print "请输入数字 N,然后按 Enter: "
n = gets

# ...
i = 0
while i <= n.to_i
arr << i**2
i += 1
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)
#...
arra = []
arr.each do |i|
if i % 2 == 0
arra << i
end
end
return arra
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]
7 changes: 5 additions & 2 deletions 20-sorting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
# Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复

def filter_even(arr)
#...
arra = []
arra = arr.uniq.sort
return arra
end

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


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

def selection_sort(arr)
#...
n = arr.size
for i in 0..(n - 1)
for j in (i + 1)..(n - 1)
if arr[i] >= arr[j]
tem = arr[j]
arr[j] = arr[i]
arr[i] = tem
end
end
end
return 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]
8 changes: 7 additions & 1 deletion 22-missing.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# 给定一阵列内含数字,请输出 0~9 中不见的数字

def find_missing(arr)
# ...
arra = []
for i in 0..9
if not arr.include? i
arra << i
end
end
return arra
end

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