From 67eec470b33b5f7c86d30717125380dca25ef718 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Thu, 31 Aug 2017 16:17:02 +0800 Subject: [PATCH 01/21] zhoujin gets puts --- 01-hello.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..d9b47f4 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" From 4285b04d02ad47684c3cc4beaf714ae0c545e81a Mon Sep 17 00:00:00 2001 From: zhoujin Date: Thu, 31 Aug 2017 16:23:46 +0800 Subject: [PATCH 02/21] zhoujin gets puts --- 01-hello.rb | 2 +- 02-variable.rb | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/01-hello.rb b/01-hello.rb index d9b47f4..e04b69e 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "hello,zhoujin" +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}" - From fb226891d9dd32bb0f2f51c57abce0d787226db3 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Thu, 31 Aug 2017 22:18:09 +0800 Subject: [PATCH 03/21] add triangle --- 03-triangle.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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}" From a76afcc3d07e953031c5cf5e28140292bbe2c6b7 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Thu, 31 Aug 2017 22:25:02 +0800 Subject: [PATCH 04/21] add pizzas --- 04-pizzas.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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}片" From 184dd9d227d3b19a0795833812206c685d883bef Mon Sep 17 00:00:00 2001 From: zhoujin Date: Thu, 31 Aug 2017 22:42:45 +0800 Subject: [PATCH 05/21] add if elseif else --- 05-bmi.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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 From 22ebfb95b74b687bb73dfdfd4693d7913737becd Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 09:35:46 +0800 Subject: [PATCH 06/21] add x y z --- 06-interger-positive.rb | 16 +++++++++++++--- 07-abcde.rb | 11 +++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) 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..7afe432 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -9,14 +9,17 @@ # 当 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 "" From 7a91a53c9d480547d9cf593c53c7d17b5f8c11b1 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 09:47:22 +0800 Subject: [PATCH 07/21] abcde --- 07-abcde.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/07-abcde.rb b/07-abcde.rb index 7afe432..57fc8c9 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -22,4 +22,17 @@ if x > 0 if y > 0 if z > 0 - puts "" + 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 From 38ede69d06ff422b090cb0c0ddaec622f5b4bf6d Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 15:22:17 +0800 Subject: [PATCH 08/21] add x y z --- 08-find-max.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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 From ba017f97773a4ac212d994730f8d60c4c0db37ac Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 16:11:21 +0800 Subject: [PATCH 09/21] calculate_area --- 09-function.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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}" From 2721f23de29504d0efc581f294606a8002996bc5 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 16:14:13 +0800 Subject: [PATCH 10/21] find max --- 10-function.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/10-function.rb b/10-function.rb index bb450fb..1c0178a 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,13 @@ # 题目: 使用者输入 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: " @@ -16,4 +23,4 @@ def find_max(x, y, z) answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From 70d5cc5473318f64b2b9f817134717a3cc729484 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 16:15:35 +0800 Subject: [PATCH 11/21] find max --- 10-function.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/10-function.rb b/10-function.rb index 1c0178a..708262e 100644 --- a/10-function.rb +++ b/10-function.rb @@ -11,13 +11,13 @@ def find_max(x, y, z) 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 # .... From dcc67a275b6326755a8bb54dc3a76d33d0319fad Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 17:22:05 +0800 Subject: [PATCH 12/21] add i --- 11-seven.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 From a4600692bae5db9431f1381f8df4463cb94f4e45 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 17:29:50 +0800 Subject: [PATCH 13/21] add total --- 12-sum-even.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 From 524e7362551739044d91695927dbc5f28f3422f8 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 17:39:51 +0800 Subject: [PATCH 14/21] add N*N --- 13-nn.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 - From c2286209ecf44176c605c7d4c8e923e58ab6c4eb Mon Sep 17 00:00:00 2001 From: zhoujin Date: Fri, 1 Sep 2017 18:26:43 +0800 Subject: [PATCH 15/21] add target --- 14-prime.rb | 11 ++++++++++- 15-guess-number.rb | 14 +++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) 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 From faf438cfb3612852a6ea6287bcced50912c9f64c Mon Sep 17 00:00:00 2001 From: zhoujin Date: Sat, 2 Sep 2017 10:12:28 +0800 Subject: [PATCH 16/21] add find_max --- 16-array-sum.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 - From ffe5285d92f7915b8fe7bdda9ac4c93bb2adfcee Mon Sep 17 00:00:00 2001 From: zhoujin Date: Sat, 2 Sep 2017 10:35:44 +0800 Subject: [PATCH 17/21] array sum average max min --- 17-array-stats.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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}" From 9b844ad1e74063545ad90200cb968c003c1f2c09 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Sat, 2 Sep 2017 10:54:47 +0800 Subject: [PATCH 18/21] array --- 18-square.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 From eac93f0b6d504190c252cca3d75e69a79f712125 Mon Sep 17 00:00:00 2001 From: zhoujin Date: Sat, 2 Sep 2017 11:00:02 +0800 Subject: [PATCH 19/21] add filter even --- 19-filter.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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] From 417f901179a36e4eb5aaaa01649450105eafa76d Mon Sep 17 00:00:00 2001 From: zhoujin Date: Sat, 2 Sep 2017 11:07:47 +0800 Subject: [PATCH 20/21] array uniq sort --- 20-sorting.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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] From c2602a823956cfbdbdb8c8c69ae49bbcc6f2a62f Mon Sep 17 00:00:00 2001 From: zhoujin Date: Thu, 7 Sep 2017 15:38:36 +0800 Subject: [PATCH 21/21] add time --- 21-selection-sort.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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]