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, ranty)"
5 changes: 3 additions & 2 deletions 02-variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

a = 1
b = 2

temp = a
a = b
b = temp
puts "a 是 #{a}"
puts "b 是 #{b}"

# ...

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

7 changes: 5 additions & 2 deletions 03-triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

print "请输入直角三角形的高,然后按 Enter: "
a = gets
c = a.to_i

print "请输入直角三角形的底边,然后按 Enter: "
b = gets

d = b.to_i
# .....

puts "直角三角形的面积是: _________"
area = (c * d)/2

puts "直角三角形的面积是: #{area}\n"
11 changes: 7 additions & 4 deletions 04-pizzas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

print "请输入有多少片比萨饼,然后按 Enter: "
pizzas = gets

a = pizzas.to_i
print "请输入有多少人要吃,然后按 Enter: "
people = gets

b = people.to_i
# .....

puts "每人可分得几片: _________ 片"
puts "还剩下几片: _________ 片"
c = a / b
d = c.to_i
e = a - (d * b)
puts "每人可分得几片: ____#{d}_____ 片"
puts "还剩下几片: ____#{e}_____ 片"
17 changes: 13 additions & 4 deletions 05-bmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

print "请输入您的体重(公斤),然后按 Enter: "
weight = gets

a = weight.to_i
print "请输入您的身高(厘米),然后按 Enter: "
height = gets

b = height.to_i
# .....
bmi = a / (b*b/10000)


puts "您的 BMI 是: _________"
if bmi < 18.5
bmi_target = "过轻"
elsif bmi < 24
bmi_target = "正常"
else
bmi_target = "过重"
end
puts "您的 BMI 是: ____#{bmi}_____"

puts "您的 BMI 结果是: _________(过轻或正常或过重)"
puts "您的 BMI 结果是: ____#{bmi_target}_____(过轻或正常或过重)"
21 changes: 18 additions & 3 deletions 06-interger-positive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@

print "请输入一个整数,然后按 Enter: "
x = gets

y = x.to_i
# ....

puts "这个数是_____ (正数或零或负数)"
puts "这个数是_____ (偶数或奇数)"
if y < 0
num = "负数"
elsif y == 0
num = "零"
else
num = "正数"
end

puts "这个数是__#{num}___ (正数或零或负数)"

math = y % 2
if math == 0
anwser = "偶数"
else
anwser = "奇数"
end
puts "这个数是__#{anwser}___ (偶数或奇数)"
17 changes: 16 additions & 1 deletion 07-abcde.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,20 @@
z = gets

# ....
x = x.to_i
y = y.to_i
z = z.to_i

puts "结果是________(A或B或C或D或E)"
if x < 0
print result = "A"
elsif (x > 0) && (y > 0) && (z > 0)
print result = "B"
elsif (x > 0) && (y > 0) && (z < 0)
print result = "C"
elsif (x > 0) && (y < 0) && (z > 0)
print result = "D"
elsif (x > 0) && (y < 0) && (z < 0)
print result = "E"
end

puts "是结果!"
5 changes: 3 additions & 2 deletions 08-find-max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
print "请输入一个数字y,然后按 Enter: "
y = gets

print "请输入一个数字z,然后按 Enter: "
print "请输入一个数nn 字z,然后按 Enter: "
z = gets

# ....

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

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

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

answer = calculate_area(a,b)

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

def find_max(x, y, z)
x = x.to_f
y = y.to_f
z = z.to_f
find_max = [x, y, z].max
end

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

answer = find_max(x,y,z)

puts "最大的数是 #{answer}"
puts "最大的数是 #{answer}"
9 changes: 6 additions & 3 deletions 11-seven.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# 题目: 列出 1 到 100 之间,所有 7 的倍数

i = 1
while ( i <= 100 )

# ....
while ( i <= 100 )

i+=1
end
if i % 7 == 0
puts i
end

end
6 changes: 4 additions & 2 deletions 12-sum-even.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
total = 0

while ( i <= 100 )

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

i+=1
end

puts total
puts total
13 changes: 12 additions & 1 deletion 13-nn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@
#
# end
# 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
14 changes: 13 additions & 1 deletion 14-prime.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# 输入一个数字 N,请检查是不是质数

def is_prime(n)
# ....
i = 2
while (i <= n/2)
a = n % i
if a != 0
i+=1
else
break
return false
end
end
if i > n / 2
return true
end
end

print "请输入数字 N,然后按 Enter: "
Expand Down
9 changes: 6 additions & 3 deletions 15-guess-number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
while (true)
print "请猜一个 0~99 的数字 N,然后按 Enter: "
n = gets
if n.to_i < target
puts "太低了,再猜一次"
elsif n.to_i > target
puts "太高了,再猜一次"
end

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

if n.to_i == target
puts "恭喜猜中啦! "
break
end

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

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

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

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

11 changes: 7 additions & 4 deletions 17-array-stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
arr << user_input.to_i
end
end
sum = 0
arr.each { |i| sum += i }
average = sum/arr.length

puts arr.to_s

puts "总和是 _____"
puts "平均是 _____"
puts "最大值是 _____"
puts "最小值是 _____"
puts "总和是 __#{sum}___"
puts "平均是 __#{average}___"
puts "最大值是 __#{arr.max}___"
puts "最小值是 __#{arr.min}___"
8 changes: 5 additions & 3 deletions 18-square.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
arr = []

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

# ...

puts arr.to_s
(0..n).each_with_index do |i, j|
arr << j ** 2
end
puts arr.to_s
12 changes: 10 additions & 2 deletions 19-filter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# 给定一阵列内含数字,输出另一个数组只包含偶数

def filter_even(arr)
#...
new_arr = []

arr.each do |i|
if i % 2 == 0
new_arr << i
end
end
print "#{new_arr}"

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]
13 changes: 11 additions & 2 deletions 20-sorting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
# Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复

def filter_even(arr)
#...
new_arr = []

arr.each do |i|
if i % 2 == 0
new_arr << i
end
end

print "#{new_arr.sort.uniq}"

end

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


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

def insertion_sort(arr)
#...
(0...arr.length).each do |i|
min, index = arr[i], i
(i...arr.length).each { |j| min, index = arr[j], j if arr[j] < min }
arr[i], arr[index] = arr[index], arr[i]
end
arr
end

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

answer = insertion_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]
Loading