diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..fe34e8d 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts "Hello,#{your_name}" diff --git a/02-variable.rb b/02-variable.rb index a5a4753..00ad6ba 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -8,6 +8,8 @@ # ... +a = b +c = a +b = c puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..30d1d8c 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -8,4 +8,4 @@ # ..... -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{a.to_f*b.to_f/2}" diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..340791d 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -8,5 +8,5 @@ # ..... -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得几片: #{pizzas.to_i/people.to_i} 片" +puts "还剩下几片: #{pizzas.to_i % people.to_i} 片" diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..dec7be5 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -9,9 +9,19 @@ print "请输入您的身高(厘米),然后按 Enter: " height = gets - +bmi = weight.to_f/((height.to_f/100)**2) # ..... -puts "您的 BMI 是: _________" +puts "您的 BMI 是: #{bmi}" + +if bmi <18.5 + a = "过轻" + +elsif bmi >= 24 + a = "过重" + + else + a = "正常" +end -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +puts "您的 BMI 结果是:#{a}" diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..e83acc2 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -5,6 +5,21 @@ x = gets # .... +if x.to_i> 0 + a = "正数" +elsif x.to_i== 0 + a = "零" +else + a = "负数" +end -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file + +if x.to_i % 2 == 0 + b = "偶数" + else + b = "奇数" +end + + +puts "这个数是 #{a}" +puts "这个数是 #{b}" diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..e1689a0 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -19,4 +19,23 @@ # .... -puts "结果是________(A或B或C或D或E)" \ No newline at end of file + +if x.to_i < 0 + a = "A" + elsif x.to_i > 0 + if y.to_i > 0 + if z.to_i > 0 + a = "B" + elsif z.to_i < 0 + a = "C" + end + elsif y.to_i < 0 + if z.to_i > 0 + a = "D" + elsif z.to_i < 0 + a = "E" + end + end + end + + puts "结果是#{a}" diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..9450625 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -11,4 +11,23 @@ # .... -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +a = x.to_f + b = y.to_f + c = z.to_f + + if a >= b + if a >= c + big = x + else + big = z + end + else + if b >= c + big = y + else + big = z + end + end + + + puts "最大的数是 #{big}" diff --git a/09-function.rb b/09-function.rb index b1f922d..d5efec8 100644 --- a/09-function.rb +++ b/09-function.rb @@ -2,6 +2,7 @@ def calculate_area(a, b) # .... + a.to_f*b.to_f/2 end print "请输入直角三角形的高,然后按 Enter: " @@ -12,4 +13,4 @@ def calculate_area(a, b) answer = calculate_area(a,b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" diff --git a/10-function.rb b/10-function.rb index bb450fb..00027b7 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,25 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + a = x.to_f + b = y.to_f + c = z.to_f + + if a >= b + if a >= c + big = x + else + big = z + end + else + if b >= c + big = y + else + big = z + end + end + + return big end print "请输入一个数字x,然后按 Enter: " @@ -16,4 +35,4 @@ def find_max(x, y, z) answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" diff --git a/11-seven.rb b/11-seven.rb index 26c221d..5f57f12 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -2,8 +2,8 @@ i = 1 while ( i <= 100 ) - - # .... - + if i%7 == 0 + puts i + end i+=1 -end \ No newline at end of file +end diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..73e3966 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -4,10 +4,12 @@ total = 0 while ( i <= 100 ) - +if i%2 == 0 + total += i + end # .... i+=1 end -puts total \ No newline at end of file +puts total diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..9408d05 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -2,6 +2,7 @@ print "请输入数字 N,然后按 Enter: " n = gets +m = n.to_i # while ( ... ) # while ( ...) @@ -9,3 +10,13 @@ # end # end + +i = 0 +while (i<=m) + j = 0 + while (j<=m) + print i,"x",j, "=",i*j,"\n" + j += 1 + end + i += 1 + end diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..b3148dd 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -2,6 +2,28 @@ def is_prime(n) # .... +res = [1] + res< 2 + return false + else + return true + end end print "请输入数字 N,然后按 Enter: " diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..2106675 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -9,9 +9,20 @@ #puts "太低了,再猜一次" #puts "太高了,再猜一次" - if n.to_i == target - puts "恭喜猜中啦! " - break + if n.to_i < target + puts "太低了,再猜一次! " + print "请猜一个 n~99的数字N,然后按Enter: " end -end \ No newline at end of file + + if n.to_i > target + puts "太高了,再猜一次" + print "请猜一个target~99的数字N,然后按Enter: " + end + +if n.to_i == target + puts "恭喜猜中啦! " +break +end + +end diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..5af3f7a 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,10 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + max = array.sort.last end arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..29766a0 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -14,7 +14,20 @@ puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +sum = 0 + i = arr.length + arr.each do |i| + sum += i + end + + ave = sum/i + + max = arr.sort.last + min = arr.sort.first + + + + puts "总和是 #{sum}" + puts "平均是 #{ave}" + puts "最大值是 #{max}" + puts "最小值是 #{min}" diff --git a/18-square.rb b/18-square.rb index 226e1c1..6f2c3ac 100644 --- a/18-square.rb +++ b/18-square.rb @@ -7,4 +7,10 @@ # ... -puts arr.to_s \ No newline at end of file +for i in 0..(n.to_i)-1 + arr<< i**2 + end + + + + puts arr.to_s diff --git a/19-filter.rb b/19-filter.rb index ef7e515..44ce470 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,16 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + result = [] + arr.each do |i| + if i % 2 == 0 + result.push(i) + end + end + + return result end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] \ No newline at end of file +puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..a64b641 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,17 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + result = [] + arr.each do |i| + if i % 2 == 0 + result.push(i) + end + end + + return result.sort.uniq end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -puts "________" # 应该是 [42, 46, 68, 86] \ No newline at end of file +puts " filter_even(arr).to_s" # 应该是 [42, 46, 68, 86] diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..2dbbe9b 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,26 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) - #... + len = arr.length - 1 + + for i in 0..len + min = i + for j in i1..len + if arr[j]< arr[min] + min = j + + temp = arr[min] + arr[min] = arr[i] + arr[i] = temp + end + end + end + + return arr end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] answer = selection_sort(arr) -puts answer.to_s # 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91] \ No newline at end of file +puts answer.to_s # 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91] diff --git a/22-missing.rb b/22-missing.rb index 6898714..4f5f76b 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -5,5 +5,7 @@ def find_missing(arr) end answer = find_missing( [2,2,1,5,8,4] ) +arr1 = [0,1,2,3,4,5,6,7,8,9] +arr2 = arr1 - arr puts answer.to_s # 应该是 [0,3,6,7,9] diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..ffddd51 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,9 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + arr = hash.values + max = arr.sort.last + return hash.key(max) end h = { @@ -15,5 +17,3 @@ def find_max(hash) answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..5af0c84 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -2,7 +2,21 @@ def find_even_keys(hash) - # ... (请回传一个数组) + value = hash.values + a = [] + for i in value + if i%2 == 0 + a<3, "d"=>6, "c"=>5, "b"=>1, "e"=>5} - diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..55b0724 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -10,7 +10,18 @@ # .... -puts "所有成年人,并由小到大: _________" +a = [] + for i in arr #这里i就是arr中的值,也就是一个个hash. + if i["age"] >= 18 + a<