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

编程基础练习簿——作业 #100

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 36 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, 张行知"
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 "b = #{a}"
puts "a = #{b}"
2 changes: 1 addition & 1 deletion 03-triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

# .....

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

# .....

puts "每人可分得几片: _________ 片"
puts "还剩下几片: _________ 片"
puts "每人可分得几片: #{pizzas.to_i / people.to_i}"
puts "还剩下几片: #{pizzas.to_i - (pizzas.to_i / people.to_i ) * people.to_i }"
14 changes: 11 additions & 3 deletions 05-bmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
print "请输入您的体重(公斤),然后按 Enter: "
weight = gets

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

# .....

puts "您的 BMI 是: _________"
puts "您的 BMI 是: #{weight.to_f / (height.to_f * height.to_f)} "
BMI = weight.to_f / (height.to_f * height.to_f)

puts "您的 BMI 结果是: _________(过轻或正常或过重)"
puts "您的 BMI 结果是:
#{if BMI < 18.5
puts '过轻'
elsif BMI >= 24
puts '过重'
else 18.5 <= BMI
puts '正常'
end}"
20 changes: 18 additions & 2 deletions 06-interger-positive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,21 @@

# ....

puts "这个数是_____ (正数或零或负数)"
puts "这个数是_____ (偶数或奇数)"
puts "这个数是
#{
if x.to_f > 0
puts "正数"
elsif x.to_f == 0
puts "零"
else x.to_f < 0
puts "负数"
end
}
"
puts "这个数是
#{
if (x.to_f / 2).to_i == x.to_f / 2
puts "偶数"
end
}
"
24 changes: 23 additions & 1 deletion 07-abcde.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@

# ....

puts "结果是________(A或B或C或D或E)"
puts "结果是
#{
if x.to_f < 0
puts "A"
end

if x.to_f > 0 && y.to_f > 0 && z.to_f > 0
puts "B"
elsif x.to_f > 0 && y.to_f > 0 && z.to_f < 0
puts "C"
else
puts ""
end

if x.to_f > 0 && y.to_f < 0 && z.to_f > 0
puts "D"
elsif x.to_f > 0 && y.to_f < 0 && z.to_f < 0
puts "E"
else
puts ""
end
}
"
12 changes: 11 additions & 1 deletion 08-find-max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@

# ....

puts "最大的数是 ________(x或y或z)"
puts "最大的数是
#{
if x.to_f > y.to_f && x.to_f > z.to_f
puts "#{x}"
elsif y.to_f > x.to_f && y.to_f > z.to_f
puts "#{y}"
else
puts "#{z}"
end
}
"
10 changes: 9 additions & 1 deletion 09-function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ def calculate_area(a, b)

answer = calculate_area(a,b)

puts "直角三角形的面积是: #{answer}"
puts "直角三角形的面积是:
#{
def calculate_area(a,b)
"#{(b.to_f * a.to_f) / 2} "
end
answer = calculate_area(a,b)
puts "#{answer}"
}
"
17 changes: 16 additions & 1 deletion 10-function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,19 @@ def find_max(x, y, z)

answer = find_max(x,y,z)

puts "最大的数是 #{answer}"
puts "最大的数是
#{
def find_max(x, y, z)
if x.to_f > y.to_f && x.to_f > z.to_f
"#{x}"
elsif y.to_f > x.to_f && y.to_f > z.to_f
"#{y}"
else
"#{z}"
end
end
answer = find_max(x,y,z)
puts "#{answer}"

}
"
15 changes: 9 additions & 6 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 )
i = 7
# while ( i < 7 )
# puts i
# i = i + 6
# end

# ....

i+=1
end
while ( i <= 100 )
puts i
i = i + 7
end
22 changes: 15 additions & 7 deletions 12-sum-even.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# 题目: 求 1~100 所有偶数的和

i = 1
i = 2
total = 0

while ( i <= 100 )

# ....

i+=1
while ( i <= 100 )
total += i
i += 2
end

puts total
puts total

# i = 0
# j = 0
#
# while i <= 100
# j += i
# i += 1
# end
#
# puts j
15 changes: 10 additions & 5 deletions 13-nn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
print "请输入数字 N,然后按 Enter: "
n = gets

# while ( ... )
# while ( ...)
#
# end
# end
a = 0 #
b = 0 #

while ( a <= n.to_i ) # 代表等式左边左侧的循环
while ( b <= n.to_i ) # 代表等式左边右侧的循环
puts "#{a} * #{b} = #{a*b}"
b += 1 #把b的值递增,否则会无限循环,没办法停止
end
a += 1 # 让a的+1
b = 0 # 必须从0开始,与开始时保持一致
end
2 changes: 1 addition & 1 deletion 14-prime.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 输入一个数字 N,请检查是不是质数

def is_prime(n)
# ....
# ...
end

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

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

if n.to_i > target
puts "太高了,再猜一次"
end

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

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

def find_max(array)
#....
def find_max(arr)
array = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88].max
end

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

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

max = find_max(Array)
puts "Max is #{max}" # 应该是 88
24 changes: 20 additions & 4 deletions 17-array-stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,23 @@

puts arr.to_s

puts "总和是 _____"
puts "平均是 _____"
puts "最大值是 _____"
puts "最小值是 _____"
puts "总和是
#{
arr.inject(:+)
}
"
puts "平均是
#{
arr.inject(:+)/arr.size
}
"
puts "最大值是
#{
arr.sort[-1]
}
"
puts "最小值是
#{
arr.sort[0]
}
"
9 changes: 6 additions & 3 deletions 18-square.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# 建构一个阵列有一百个的元素,内容是 0, 1, 4, 9, 16, 25...... 每个元素是该索引的平方

arr = []

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

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

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

def filter_even(arr)
#...
end

#
# def filter_even(arr)
#
# end


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

arr.each do |i|
if i % 2 == 0
puts i # 这部分不知道怎么输出数组了?
end
end

# puts arr.push(x).to_s

puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86]
# puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86]
9 changes: 8 additions & 1 deletion 20-sorting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ def filter_even(arr)
end

arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1]
a = []
arr.each do |i|
if i % 2 == 0
a.push(i).to_s
end
end

puts a.sort.uniq

puts "________" # 应该是 [42, 46, 68, 86]
# puts "________" # 应该是 [42, 46, 68, 86]
4 changes: 2 additions & 2 deletions 21-selection-sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ def selection_sort(arr)

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

answer = selection_sort(arr)
answer = arr.sort

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