Skip to content
Open
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 "hollo, #{your_name}"
5 changes: 2 additions & 3 deletions 02-variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@

# ...

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

puts "a 应该是 #{a},现在是 #{b}"
puts "b 应该是 #{b},现在是 #{a}"
4 changes: 2 additions & 2 deletions 03-triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
b = gets

# .....

puts "直角三角形的面积是: _________"
c = a.to_i * b.to_i / 2
puts "直角三角形的面积是: #{c}"
6 changes: 4 additions & 2 deletions 04-pizzas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@

# .....

puts "每人可分得几片: _________ 片"
puts "还剩下几片: _________ 片"
a = pizzas.to_i / people.to_i
b = pizzas.to_i % people.to_i
puts "每人可分得几片: #{a} 片"
puts "还剩下几片: #{b} 片"
16 changes: 14 additions & 2 deletions 05-bmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@

# .....

puts "您的 BMI 是: _________"
a = weight.to_i
b = height.to_f / 100
c = a / (b * b)

puts "您的 BMI 结果是: _________(过轻或正常或过重)"
if c < 18.5
d = "过轻"
elsif c >= 24
d = "过重"
else d = "正常"

end

puts "您的 BMI 是: #{c}"

puts "您的 BMI 结果是: #{d}"
22 changes: 20 additions & 2 deletions 06-interger-positive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
x = gets

# ....
a = x.to_i

puts "这个数是_____ (正数或零或负数)"
puts "这个数是_____ (偶数或奇数)"
if a > 0
b = "正数"
elsif a == 0
b = "零"
else b = "负数"

end
if a > 0

if a % 2 == 0
c = "偶数"
else
c = "奇数"
end

end

puts "这个数是: #{b}"
puts "这个数是: #{c}"
22 changes: 21 additions & 1 deletion 07-abcde.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
print "请输入一个整数x,然后按 Enter: "
x = gets


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

Expand All @@ -19,4 +20,23 @@

# ....

puts "结果是________(A或B或C或D或E)"
a = x.to_f
b = y.to_f
c = z.to_f

if a < 0
h = "A"
elsif a > 0 && b > 0 && c > 0
h = "B"
elsif a > 0 && b > 0 && c < 0
h = "C"
elsif a > 0 && b < 0 && c > 0
h = "D"
elsif a > 0 && b < 0 && c < 0
h = "E"
end




puts "结果是:#{h}"
19 changes: 18 additions & 1 deletion 08-find-max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,21 @@

# ....

puts "最大的数是 ________(x或y或z)"
x = x.to_f
y = y.to_f
z = z.to_f

# if x > y && x > z
# h = "x"
# elsif y > x && y > z
# h = "y"
# elsif z > x && z > y
# h = "z"
# else
# puts "无法求解"
#
# end

h = [x, y, z].max.to_i

puts "最大的数是: #{h}"
11 changes: 7 additions & 4 deletions 09-function.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# 题目: 输入直角三角形的宽和高,输出三角形的面积

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

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

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




answer = calculate_area(a,b)

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

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

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

answer = find_max(x,y,z)

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

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 @@ -4,10 +4,12 @@
total = 0

while ( i <= 100 )

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

i+=1
end

puts total
puts total
12 changes: 11 additions & 1 deletion 13-nn.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
# 题目: 输入一个数字 N,输出 N * N 乘法表

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

# while ( ... )
# while ( ...)
#
# end
# end
i = 0
while (i <= n)
j = 0
while (j <= n)
h = i * j
puts "#{i} × #{j} = #{h}"
j += 1
end

i += 1
end
9 changes: 9 additions & 0 deletions 14-prime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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

end

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

#puts "太低了,再猜一次"
#puts "太高了,再猜一次"
if n.to_i < target
puts "太低了,再猜一次"
elsif n.to_i > target
puts "太高了,再猜一次"

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

end
end
8 changes: 7 additions & 1 deletion 16-array-sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

def find_max(array)
#....
# array.max
b = 0
array.each do |a|
if b < a
b = a
end
end
end

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

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

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

sum = 0
arr.each { |i| sum += i}
average = sum/arr.size.to_f




puts arr.to_s

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

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

# ...
# i = 0
# while i < n
# arr << i * i
# i += 1
# end

puts arr.to_s
(0..n).each_with_index do |i, j|
arr << j ** 2
end

puts arr.to_s
10 changes: 9 additions & 1 deletion 19-filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

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

arr.each do |i|
if i % 2 == 0
new_array << i
end
end
print "#{new_array}"
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_array = []

arr.each do |i|
if i % 2 == 0
new_array << i
end
end
print "#{new_array.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]
Loading