diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..e04b69e 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -1,8 +1,8 @@ # 题目: 输入名字,输出 "Hello, 名字" print "请输入你的名字,然后按 Enter: " -your_name = gets +zhoujin = gets # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts "hello,#{zhoujin}" diff --git a/02-variable.rb b/02-variable.rb index a5a4753..3ec25fd 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -3,11 +3,10 @@ a = 1 b = 2 -puts "a 是 #{a}" -puts "b 是 #{b}" +puts "a = #{b}" +puts "b = #{a}" # ... puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..2362a80 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -7,5 +7,5 @@ b = gets # ..... - -puts "直角三角形的面积是: _________" \ No newline at end of file +s = a.to_f * b.to_f / 2 +puts "直角三角形的面积是:#{s}" diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..409537a 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -7,6 +7,7 @@ people = gets # ..... - -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +a = pizzas.to_i / people.to_i +b = pizzas.to_i % people.to_i +puts "每人可分得几片: _#{a}片" +puts "还剩下几片:#{b}片" diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..cf1b10c 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -11,7 +11,18 @@ height = gets # ..... +bmi = ( weight.to_f * 10000/ ( height.to_f * height.to_f ) ) +puts "您的 BMI 是: #{bmi}" -puts "您的 BMI 是: _________" -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file + + +if bmi < 18.5 + puts "您的BMI结果是:过轻" + +elsif bmi >= 24 + puts "您的BMI结果是:过重" + +else + puts "您的BMI结果是:正常" +end diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..ff3b464 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -4,7 +4,17 @@ print "请输入一个整数,然后按 Enter: " x = gets -# .... -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if x.to_i > 0 + puts "这个数是正数" +elsif x.to_i == 0 + puts "这个数是零" +else + puts "这个数是负数" +end + +if x.to_i % 2 == 0 + puts "这个数是偶数" +else + puts "这个数是奇数" +end diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..57fc8c9 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -9,14 +9,30 @@ # 当 z < 0 输出 "E" print "请输入一个整数x,然后按 Enter: " -x = gets +x = gets.to_i print "请输入一个整数y,然后按 Enter: " -y = gets +y = gets.to_i print "请输入一个整数z,然后按 Enter: " -z = gets +z = gets.to_i # .... -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +if x > 0 + if y > 0 + if z > 0 + puts "结果是B" + elsif z < 0 + puts "结果是C" + end + elsif y < 0 + if z > 0 + puts "结果是D" + elsif z < 0 + puts "结果是E" + end + end +elsif x < 0 + puts "结果是A" +end diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..cbfcb53 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -11,4 +11,11 @@ # .... -puts "最大的数是 ________(x或y或z)" \ No newline at end of file + +if x >= y && x >= z + puts "最大的数是x" +elsif y >= x && y >= z + puts "最大的数是y" +else + puts "最大的数是z" +end diff --git a/09-function.rb b/09-function.rb index b1f922d..8d9e6c8 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,15 +1,15 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 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}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" diff --git a/10-function.rb b/10-function.rb index bb450fb..708262e 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,19 +1,26 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + if x >= z && x >= z + return x + elsif y >= x && y >= z + return y + else + return z + end end print "请输入一个数字x,然后按 Enter: " -x = gets +x = gets.to_f print "请输入一个数字y,然后按 Enter: " -y = gets +y = gets.to_f print "请输入一个数字z,然后按 Enter: " -z = gets +z = gets.to_f # .... 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..afc26f9 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -3,7 +3,9 @@ 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..35065d0 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -5,9 +5,11 @@ 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..e209fc5 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,11 +1,19 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " -n = gets +n = gets.to_i +i = 1 +while ( i <= n ) + j = i + while ( j <= n ) + puts " #{i} * #{j} = #{i*j}" + j += 1 + end + i += 1 +end # while ( ... ) # while ( ...) # # end # end - diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..ee539ea 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,7 +1,16 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + i = 2 + while ( i <= (n/2) ) + if n % i == 0 + return false + break + else + i += 1 + end + end + return true end print "请输入数字 N,然后按 Enter: " diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..8b7d869 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -5,13 +5,13 @@ while (true) print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" - - if n.to_i == target + if n.to_i < target + puts "太低了,再猜一次" +elsif + n.to_i > target + puts "太高了,再猜一次" +else n.to_i == target puts "恭喜猜中啦! " break end - -end \ No newline at end of file +end diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..fbfcfaa 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,16 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + m = array[0] + array.each do |i| + if m <= i + m = 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 - diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..d359dfa 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 +def sum(arr) + sum = 0 + for i in arr + sum += i + end + return sum +end + +def average(arr) + return sum(arr) /arr.size +end + + +puts "总和是 #{sum(arr)}" +puts "平均是 #{average(arr)}" +puts "最大值是 #{arr.max}" +puts "最小值是 #{arr.min}" diff --git a/18-square.rb b/18-square.rb index 226e1c1..65ed3a8 100644 --- a/18-square.rb +++ b/18-square.rb @@ -3,8 +3,13 @@ arr = [] print "请输入数字 N,然后按 Enter: " -n = gets +n = gets.to_i -# ... +i = 0 +while ( i < n ) + arr.push(i * i) + i += 1 +end -puts arr.to_s \ No newline at end of file + +puts arr.to_s diff --git a/19-filter.rb b/19-filter.rb index ef7e515..8295578 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,15 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + even_arr = [] + arr.each do |n| + if (n % 2 == 0) + even_arr.push(n) + end + end + return even_arr 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..d568d6f 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,16 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + even_arr = [] + arr.each do |n| + if n % 2 == 0 + even_arr.push(n) + end + end + return even_arr.uniq.sort 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..ec63a61 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,28 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) - #... + temp = 0 + i = 0 + l = arr.size - 1 + while ( i < l ) + min = i + j = i + 1 + while (j < arr.size ) + if arr[min] > arr[j] + min = j + end + j += 1 + end + temp = arr[min] + arr[min] = arr[i] + arr[i] = temp + i += 1 + 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]