Paizaラーニングの論理演算メニューのrubyでの模範解答です。
答えだけですみません。
STEP: 1 論理積( AND )の基本
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step1
a,b =gets.split(' ').map(&:to_i)
if a == 0 || b == 0
puts 0
else
puts 1
end
STEP: 2 論理和( OR )の基本
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step2
a,b = gets.split(' ').map(&:to_i)
if a == 1 || b == 1
puts 1
else
puts 0
end
STEP: 3 否定( NOT )の基本
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step3
a = gets.to_i
if a == 0
puts 1
else
puts 0
end
STEP: 4 排他的論理和( XOR )の基本
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step4
a,b = gets.split(' ').map(&:to_i)
if a == 1 && b == 1
puts 0
elsif a ==1 || b == 1
puts 1
else
puts 0
end
STEP: 5 NAND 演算の基本
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step5
a,b = gets.split(' ').map(&:to_i)
if a == 1 && b == 1
puts 0
else
puts 1
end
STEP: 6 NOR 演算の基本
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step6
a,b = gets.split(' ').map(&:to_i)
if a == 0 && b == 0
puts 1
else
puts 0
end
STEP: 7 XNOR 演算の基本
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step7
a,b = gets.split(' ').map(&:to_i)
if a == 0 && b == 0
puts 1
elsif a == 0 || b == 0
puts 0
else
puts 1
end
STEP: 8 半加算器
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step8
a,b = gets.split(' ').map(&:to_i)
if a == 1 && b == 1
c = 1
s = 0
elsif a == 1 || b == 1
c = 0
s = 1
else
c = 0
s = 0
end
puts "#{c} #{s}"
STEP: 9 全加算器
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step9
a,b,c = gets.split(' ').map(&:to_i)
if c == 1
if a == 1 && b == 1
d = 1
s = 1
elsif a == 1 || b == 1
d = 1
s = 0
else
d = 0
s = 1
end
else
if a == 1 && b == 1
d = 1
s = 0
elsif a == 1 || b == 1
d = 0
s = 1
else
d = 0
s = 0
end
end
puts "#{d} #{s}"
STEP:10論理演算を用いた計算のまとめ
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_boss
a,b,c,d = gets.split(' ').map(&:to_i)
if a == 0 && b == 0
e = 1
else
e = 0
end
if c == 0 || e == 1
f = 0
else
f = 1
end
if f == 1 && d == 1
puts 0
elsif f == 1 || d == 1
puts 1
else
puts 0
end