As a major premise, posting the answer is prohibited, so please use it as a reference only. I will not explain in detail because it can be cleared without using arrays and hashes. In addition, the poster is a fledgling inexperienced person and is currently ranked B, and it is undeniable that he lacks knowledge. Please note that the detailed method explanation is omitted.
Input example 1(input)
10
##Get 10 as a number
input = gets.chomp.to_i
=> 10
##Get 10 as a string
input = gets.chomp.to_s
=> "10"
##Divide 10 and get as a numerical value
input = gets.chomp.split("").map(&:to_i)
=> [1, 0]
##Divide 10 and get as a numerical value
input = gets.chomp.split("").map(&:to_s)
=> ["1", "0"]
Input example 2(input)
10 20
## 10,Get 20 as a number
input = gets.chomp.split.map(&:to_i)
=> [10, 20]
## 10,Get 20 as a string
input = gets.chomp.split.map(&:to_s)
=> ["10", "20"]
Input example 3(input)
1
2
3
## 1,2,Get 3 as a number
input = readlines.map(&:chomp).map(&:to_i)
=> [1, 2, 3]
## 1,2,Get 3 as a string
input = readlines.map(&:chomp)
=> ["1", "2", "3"]
I don't recommend the readlines method because it becomes difficult to use when it comes to B and C ranks.
Output example 1
input = 1
##Output 1 as a number
puts input
=> 1
Output example 2
input = ["1", "2", "3"]
## 1,2,Output 3 as a number
puts input.map(&:to_i)
=> [1, 2, 3]
##Output only 1 as a character string
puts input[0]
=> "1"
##Output only 3 as a numerical value
puts input[2].to_i
=> 3
Output example 3
input1, input2, input3 = ["1", "2", "3"]
##Output only 2 as a character string
puts input2
=> "2"
##Output only 1 as a numerical value
puts input1.to_i
=> 1
## "1 2 3"Output as the character string
puts input1 + " " + input2 + " " + input3
Or
puts "#{input1} #{input2} #{input3}"
=> "1 2 3"
If you are not aware of the numerical values and character strings, you will not be able to output as you want, so be careful.
Returns the result of evaluating the block for each element as a new array.
input = [1, 2, 3]
puts input.map { |n| n * 2 }
=> [2, 4, 6]
input = ["1", "2", "3"]
puts input.map { |n| n.to_i }
Or
puts input.map(&:to_i)
=> [1, 2, 3]
Returns a string that concatenates each element.
input = ["a", "b", "c"]
puts input.join
=> "abc"
puts input.join(",")
=> "a,b,c"
puts input.join(" or ")
=> "a or b or c"
Returns the specified own element.
input = ["a", "b", "c"]
puts input.slice(1)
=> "b"
##Get 1 to 2
puts input.slice(1, 2)
=> ["b", "c"]
##Destructive method
input.slice!(0)
puts input
=> ["b", "c"]
input.slice!(1, 2)
puts input
=> "a"
Searches to the right from the string index and returns the leftmost index of the first substring found. If not found, returns nil.
input = [1, 2, 3, 4, 5]
puts input.index(2)
=> 1
puts input.index("2")
=> nil
input = "111111"
## "1"Search from the third string
puts input.index("1", 3)
=> 3
Returns a string with all lowercase letters replaced by the corresponding uppercase letters. (upcase method) Returns a string with all uppercase letters replaced by the corresponding lowercase letters. (downcase method)
input = "abcd 1234 GHJK !!"
puts input.upcase
=> "ABCD 1234 GHJK !!"
input = "abcd 1234 GHJK !!"
puts input.downcase
=> "abcd 1234 ghjk !!"
Generates and returns a string that replaces the first matching part of the string. The gsub method will generate and return a string that replaces all the matched parts.
input = "aaaabbbb11112222"
puts input.sub("a", "A")
=> "Aaaabbbb11112222"
puts input.gsub("a", "A")
=> "AAAAbbbb11112222"
puts input.gsub("a", "A").gsub("1", "3")
=> "AAAAbbbb33332222"
Returns the number of characters in the string.
input = "aaaabbbb11112222"
puts input.length
=> 16
input = "aaaa bbbb 1111 2222 !"
puts input.length
=> 21
Returns the number of elements in the receiver.
input = [1, 2, 3, 4, 4]
puts input.count
=> 5
puts input.count(4)
=> 2
##Divide each element by 2 and count only 0
puts input.count {|x| x % 2 == 0}
=> 3
Returns a string that is a left-right inverted string for each character.
input = "paiza Drank 12"
puts input.reverse
=> "21 knarD aziap"
input = ["1", 2, "3", "true"]
puts input.reverse
=> ["true", "3", 2, "1"]
Removes the first element of the array and returns it. If an argument is specified, that number is removed and it is returned as an array.
input = [1, 2, 3, 4, 5]
puts input.shift
=> 1
puts input
=> [2, 3, 4, 5]
input = ["a", "b", "c", "d"]
puts input.shift(3)
=> ["a", "b", "c"]
puts input
=> ["d"]
Returns the sum of the elements.
input = [1, 2, 3, 4, 5]
puts input.sum
=> 15
input = ["12", "45"]
p input.sum
=>error
Returns the largest element. (max method) Returns the smallest element. (min method)
input = [1, 2, 3, 4, 5]
puts input.max
=> 5
input = [1, 2, 3, 4, 5]
puts input.min
=> 1
input = ["abcde", "123", "ABCD"]
puts input.max
=> "abcde"
input = ["abcde", "123", "ABCD"]
puts input.min
=> "123"
Returns true if it is odd. Otherwise it returns false. (odd? method) Returns true if it is even. Otherwise it returns false. (even? method)
puts 5.odd?
=> true
puts 2.odd?
=> false
puts 2.even?
=> true
puts 5.even?
=> false
Returns the absolute value of the value.
puts 100.abs
=> 100
puts -100.abs
=> 100
Returns the integer or real number closest to itself. It is so-called rounding, but it is not even rounding.
input = 3.141592
puts input.round
=> 3
##Rounds off to the third decimal place and returns the value
puts input.round(2)
=> 3.14
##Rounds the 4th decimal place and returns the value
puts input.round(3)
=> 3.142
Returns the value with the decimal point rounded up
input = 3.141592
puts input.ceil
=> 4
##Rounds up to the third decimal place and returns the value
puts input.ceil(2)
=> 3.15
##Round up the 4th decimal place and return the value
puts input.ceil(3)
=> 3.142
Returns the value with decimal point truncation
input = 3.141592
puts input.floor
=> 3
##Returns the value with the third decimal place truncated
puts input.floor(2)
=> 3.14
##Returns the value with the 4th decimal place truncated
puts input.floor(3)
=> 3.141
I would be honored if you could help me even a little. If you have any mistakes, please leave a comment. If I have time, I plan to summarize the methods used in C rank!
Recommended Posts