Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b87df5e
complete 01-hello.rb
youmeiwu Sep 30, 2017
9ecbad8
complete 02-variable.rb
youmeiwu Sep 30, 2017
08b2514
complete 03-triangle.rb
youmeiwu Sep 30, 2017
9af4ace
complete 04-pizzas.rb
youmeiwu Oct 1, 2017
8aa7f68
complete 05-bmi.rb
youmeiwu Oct 1, 2017
074515f
complete 06-interger-positive.rb
youmeiwu Oct 1, 2017
3489e93
complete 07-abcde.rb
youmeiwu Oct 1, 2017
8d67de7
complete 08-find-max.rb
youmeiwu Oct 1, 2017
83753c1
09-function.rb
youmeiwu Oct 1, 2017
ee6ddb3
complete 10-function.rb
youmeiwu Oct 1, 2017
0851366
complete 11-seven.rb
youmeiwu Oct 1, 2017
00a53dc
complete 12-sum-even.rb
youmeiwu Oct 1, 2017
99682b9
complete 13-nn.rb
youmeiwu Oct 1, 2017
d3f12c7
complete 14-prime.rb
youmeiwu Oct 1, 2017
56d132c
complete 15-guess-number.rb
youmeiwu Oct 1, 2017
4e5a000
complete 16-arrary-sum
youmeiwu Oct 2, 2017
07831a7
complete ruby 17-array-stats.rb
youmeiwu Oct 2, 2017
47805e3
complete 18-square.rb
youmeiwu Oct 2, 2017
9a2da08
complete 19-filter.rb
youmeiwu Oct 2, 2017
aa68ea7
complete 20-sorting.rb
youmeiwu Oct 2, 2017
00dc916
complete 21-selection-sort.rb
youmeiwu Oct 2, 2017
2bd53c2
complete 22-missing.rb
youmeiwu Oct 2, 2017
0e911c5
complete 23-hash-max.rb
youmeiwu Oct 2, 2017
8aed51f
complete 24-hash.rb
youmeiwu Oct 2, 2017
99da256
complete 25-hash-count.rb
youmeiwu Oct 2, 2017
3f92e19
complete 26-hash-filter.rb
youmeiwu Oct 2, 2017
43936b2
complete 27-class.rb
youmeiwu Oct 2, 2017
44e5d36
complete 28-word-count.rb
youmeiwu Oct 3, 2017
0da9578
complete 29-todos.rb
youmeiwu Oct 3, 2017
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
3 changes: 2 additions & 1 deletion 01-hello.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# 题目: 输入名字,输出 "Hello, 名字"

print "请输入你的名字,然后按 Enter: "

your_name = gets

# ...

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 @@ -2,6 +2,10 @@

a = 1
b = 2
c = a
a = b
b = c


puts "a 是 #{a}"
puts "b 是 #{b}"
Expand All @@ -10,4 +14,3 @@

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 @@ -6,6 +6,7 @@
print "请输入直角三角形的底边,然后按 Enter: "
b = gets

c = a.to_f * b.to_f / 2
# .....

puts "直角三角形的面积是: _________"
puts "直角三角形的面积是: #{c}"
7 changes: 5 additions & 2 deletions 04-pizzas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
print "请输入有多少人要吃,然后按 Enter: "
people = gets

a = pizzas.to_f / people.to_i

b = pizzas.to_f % people.to_i
# .....

puts "每人可分得几片: _________ 片"
puts "还剩下几片: _________ 片"
puts "每人可分得几片: #{a.floor} 片"
puts "还剩下几片: #{b} 片"
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 * height.to_f)*10000

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

# .....

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

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

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

if y > 0
a = "正数"
if y%2 == 0
b = "偶数"
else
b = "奇数"
end
elsif y == 0
a = "零"
b = "偶数"
else
a = "负数"
if y%2 == 0
b = "偶数"
else
b = "奇数"
end
end

# ....

puts "这个数是_____ (正数或零或负数)"
puts "这个数是_____ (偶数或奇数)"
puts "这个数是 #{a} "
puts "这个数是 #{b} "
17 changes: 16 additions & 1 deletion 07-abcde.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@

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
a = "A"
elsif ( y > 0 ) && ( z > 0 )
a = "B"
elsif ( y > 0 ) && ( z < 0 )
a = "C"
elsif ( y < 0 ) && ( z > 0 )
a = "D"
elsif ( y < 0 ) && ( z < 0 )
a = "E"
end

# ....

puts "结果是________(A或B或C或D或E)"
puts "结果是 #{a}"
13 changes: 12 additions & 1 deletion 08-find-max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

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 > y) && (x > z)
a = x
elsif (y > z) || (y == z)
a = y
else
a = z
end

# ....

puts "最大的数是 ________(x或y或z)"
puts "最大的数是 #{a}"
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
a * b / 2
end

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

answer = calculate_area(a,b)

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

def find_max(x, y, z)
x = x.to_f
y = y.to_f
z = z.to_f
if (x > y) && (x > z)
a = x
elsif (y > z) || (y == z)
a = y
else
a = z
end

return a
end

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

answer = find_max(x,y,z)

puts "最大的数是 #{answer}"
puts "最大的数是 #{answer}"
6 changes: 4 additions & 2 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
14 changes: 13 additions & 1 deletion 13-nn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

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

while a <= n
while b <= n
total = a * b
puts "#{a} x #{b} = #{total}"
b += 1
end
a += 1
b = 0
end

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

4 changes: 3 additions & 1 deletion 14-prime.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# 输入一个数字 N,请检查是不是质数

def is_prime(n)
# ....
if (n == 2) || ( (n > 2) && ( n % 2 != 0) )
return true
end
end

print "请输入数字 N,然后按 Enter: "
Expand Down
11 changes: 8 additions & 3 deletions 15-guess-number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
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
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)
#....
array.max
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

total = 0
arr.each do |i|
total += i
end

puts arr.to_s

puts "总和是 _____"
puts "平均是 _____"
puts "最大值是 _____"
puts "最小值是 _____"
puts "总和是 #{total}"
puts "平均是 #{total/arr.size}"
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 @@ -4,7 +4,16 @@

print "请输入数字 N,然后按 Enter: "
n = gets
n = n.to_i
while (true)
if n >=0
arr << (n * n)
n -= 1
else
break
end
end

# ...
arr = arr.reverse!

puts arr.to_s
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)
#...
group = []

arr.each do |i|
if (i % 2 == 0 )
group << i
end
end

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

def filter_even(arr)
#...
arr.uniq.sort
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]
Loading