如何输入整数值数组,以preceeding行+列的值? [英] How to input integer value to an array, based preceeding row + column values?
问题描述
有关这个项目之后,我应该把输入的格式如下:R1C5 + 2,其内容为在餐桌上,一排5列1,加2或者格式为:R1C2C3-5,这曰:在餐桌上,排1月2日至3日柱,减去5这是假设表中所有数字是最初所有0
。在哪里我离开的:
我无法找到一个方法来检测一个+或 - 要么在表中加/减值。此外,在提供一系列允许多个补充提供两个C的或R级的时候。例如:R1R5C2C3 + 2(行范围1 - 5,列范围2 - 3,加2)
下面是下列code:
提出'请输入:
X = gets.chompCOL = []
行= []x.chars.each_slice(2){| U | U [0] ==R?行<< U [1]:COL<< U [1]}p山坳
p行把最大#在排阵:#{} row.max
把最大#在列阵列:#{} col.max#must在返回值big_row = row.max.to_i
big_col = col.max.to_i
表= Array.new(big_row){Array.new(big_col)}
我谢谢大家的帮助!
你的功课,先生。
提出'请输入:
X = gets.chompCOL = []
行= []
标志=''
VAL =''x.chars.each_slice(2)做| U |
大小写u [0]
当R则
行<< U [1]
'C'然后当
COL<< U [1]
当+, -
标志,VAL = U [0],U [1]
其他
把输入无效。
出口
结束
结束big_row = row.max.to_i
big_col = col.max.to_i
表= Array.new(big_row){Array.new(big_col)}#初始化表为全零
table.map!做|行|
row.map! {|山口| 0}
结束rows_range = row.length == 1?行[0] ...行[0]:行[0] ...行[1]
cols_range = col.length == 1? COL [0] .. COL [0]:COL [0] .. COL [1]table.each_with_index做|排,RI |
如果rows_range.include? (RI + 1).to_s
row.each_with_index做|关口,CI |
如果cols_range.include? (CI + 1).to_s
表[RI] [CI] =(符号+ VAL).to_i
结束
结束
结束
结束#用于填充表字段中。
填充= 4#表
table.each做|一行|
row.each做|山口|
打印#{col.to_s.rjust(填充)}
结束
打印\\ n
结束
For this following project, I am supposed to take input in the following format : R1C5+2 , which reads it as "in the table, Row 1 Column 5 ,add 2. Or in this format : R1C2C3-5 , which reads : "in the table, Row 1 Column 2-3, subtract 5. This is assuming that all numbers in the table are initially all 0.
Where I left Off:
I am having trouble finding a way to detect for a "+" or "-" to either add/subtract values in the table. Also, in providing a range to allow multiple additions when provided two C's or R's. For example: R1R5C2C3+2 (Row Range 1 - 5, Column Range 2 - 3, add 2).
Here is the following code:
puts 'Please input: '
x = gets.chomp
col = []
row = []
x.chars.each_slice(2) { |u| u[0] == "R" ? row << u[1] : col << u[1] }
p col
p row
puts "Largest # in Row array: #{row.max}"
puts "Largest # in Columns array: #{col.max}" #must be in "" to return value
big_row = row.max.to_i
big_col = col.max.to_i
table = Array.new (big_row) { Array.new(big_col) }
I thank you all for the help!
Your homework, sir.
puts 'Please input: '
x = gets.chomp
col = []
row = []
sign = ''
val = ''
x.chars.each_slice(2) do |u|
case u[0]
when 'R' then
row << u[1]
when 'C' then
col << u[1]
when '+', '-'
sign, val = u[0], u[1]
else
puts 'Invalid input.'
exit
end
end
big_row = row.max.to_i
big_col = col.max.to_i
table = Array.new (big_row) { Array.new(big_col) }
# Initialize table to all zeros
table.map! do |row|
row.map! { |col| 0 }
end
rows_range = row.length == 1 ? row[0]..row[0] : row[0]..row[1]
cols_range = col.length == 1 ? col[0]..col[0] : col[0]..col[1]
table.each_with_index do |row, ri|
if rows_range.include? (ri + 1).to_s
row.each_with_index do |col, ci|
if cols_range.include? (ci + 1).to_s
table[ri][ci] = (sign + val).to_i
end
end
end
end
# Padding for fields in table.
padding = 4
# Table
table.each do |row|
row.each do |col|
print "#{col.to_s.rjust(padding)}"
end
print "\n"
end
这篇关于如何输入整数值数组,以preceeding行+列的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!