Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提交作业 #99

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
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}"
2 changes: 1 addition & 1 deletion 02-variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
puts "b 是 #{b}"

# ...
a, b = b, a

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

# .....
s = (a.to_f * b.to_f) / 2

puts "直角三角形的面积是: _________"
puts "直角三角形的面积是: ____#{s}_____"
6 changes: 4 additions & 2 deletions 04-pizzas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
people = gets

# .....
a = pizzas.to_i / people.to_i
b = pizzas.to_i % people.to_i

puts "每人可分得几片: _________ 片"
puts "还剩下几片: _________ 片"
puts "每人可分得几片: _____#{a}____ 片"
puts "还剩下几片: ____#{b}_____ 片"
16 changes: 13 additions & 3 deletions 05-bmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@

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

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

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

if bmi < 18.5
result = "过轻"
elsif bmi >= 24
result = "过重"
else
result = "正常"
end

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

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

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

x = x.to_i
# ....

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

result2 = (x % 2 == 0) ? "偶数" : "奇数"

puts "这个数是___#{result1}__ (正数或零或负数)"
puts "这个数是__#{result2}___ (偶数或奇数)"
14 changes: 13 additions & 1 deletion 07-abcde.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@

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

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

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

# ....
if x > 0
if y > 0
result = (z > 0) ? "B" : "C"
else
result = (z > 0) ? "D" : "E"
end
else
result = "A"
end

puts "结果是________(A或B或C或D或E)"
puts "结果是____#{result}____(A或B或C或D或E)"
5 changes: 3 additions & 2 deletions 08-find-max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

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

# ....

puts "最大的数是 ________(x或y或z)"
result = [x, y, z].map(&:to_i).max

puts "最大的数是 ___#{result}_____(x或y或z)"
5 changes: 4 additions & 1 deletion 09-function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

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

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

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

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].map(&:to_i).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 @@ -4,6 +4,9 @@
while ( i <= 100 )

# ....
if i % 7 == 0
puts i
end

i+=1
end
end
5 changes: 4 additions & 1 deletion 12-sum-even.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
while ( i <= 100 )

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

i+=1
end

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

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

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

i = 1
while ( i <= n )
h = 1
while ( h <= n )
puts "#{i} * #{h} = #{i * h}"
h += 1
end
i += 1
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)
# ....
if n < 2
return false
elsif n == 2
return true
else
for i in 2..n-1
if n % i == 0
return false
else
return true
end
end
end
end

print "请输入数字 N,然后按 Enter: "
Expand Down
9 changes: 5 additions & 4 deletions 15-guess-number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
print "请猜一个 0~99 的数字 N,然后按 Enter: "
n = gets

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

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

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

def find_max(array)
#....
m = array[0]
for i in 1..((array.length)-1)
if array[i] > m
m = array[i]
end
end
m
end

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

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

16 changes: 12 additions & 4 deletions 17-array-stats.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 使用者不断输入数字存进 Array,最后输出总和、平均、最大值、最小值

arr = []
total = 0

while (true)
print "请输入数字,结束请直接按 Enter: "
Expand All @@ -12,9 +13,16 @@
end
end

for i in arr
total += i
end

puts arr.to_s
average = total.to_f / arr.length
max = arr.max
min = arr.min

puts "总和是 _____"
puts "平均是 _____"
puts "最大值是 _____"
puts "最小值是 _____"
puts "总和是 __#{total}___"
puts "平均是 __#{average}___"
puts "最大值是 ___#{max}__"
puts "最小值是 ___#{min}__"
6 changes: 5 additions & 1 deletion 18-square.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

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

for i in 0..n-1
arr << i ** 2
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 = []
for i in arr
if i % 2 == 0
result << i
end
end
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)
#...
result = []
for i in arr
if i % 2 == 0
result << i
end
end
result.sort.uniq
end

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


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

def insertion_sort(arr)
#...
return arr if arr.length == 0
(0..arr.length-1).each do |i|
min, index = arr[i], i
(i+1..arr.length-1).each do |j|
min, index = arr[j], j if arr[j] < min
arr[i], arr[index] = arr[index], arr[i]
end
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]
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)
# ...
arr.uniq
result = []
for i in 0..9
result << i if !arr.include?(i)
end
result
end

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