0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニング解答: 論理演算メニュー[Ruby]

Posted at

論理演算メニュー

できるだけシンプルなコードになるよう工夫しました。

STEP: 1 論理積( AND )の基本

a, b = gets.split.map(&:to_i)
puts a & b

STEP: 2 論理和( OR )の基本

a, b = gets.split.map(&:to_i)
puts a | b

STEP: 3 否定( NOT )の基本

a = gets.to_i
puts !a.zero? ? 0 : 1

STEP: 4 排他的論理和( XOR )の基本

a, b = gets.split.map(&:to_i)
puts a ^ b

STEP: 5 NAND 演算の基本

a, b = gets.split.map(&:to_i)
puts !(a.nonzero? && b.nonzero?) ? 1 : 0

STEP: 6 NOR 演算の基本

a, b = gets.split.map(&:to_i)
puts !(a.nonzero? || b.nonzero?) ? 1 : 0

STEP: 7 XNOR 演算の基本

a, b = gets.split.map(&:to_i)
puts !(a.zero? ^ b.zero?) ? 1 : 0

STEP: 8 半加算器

a, b = gets.split.map(&:to_i)
c = a & b
s = a ^ b
puts "#{c} #{s}"

STEP: 9 全加算器

a, b, c1 = gets.split.map(&:to_i)

def half_adder(a, b)
    c = a & b
    s = a ^ b
    return [c, s]
end

cx, sy = half_adder(a, b)
cy, s  = half_adder(sy, c1)
c2 = cx ^ cy
puts "#{c2} #{s}"

FINAL問題 論理演算を用いた計算のまとめ

a, b, c, d = gets.split.map(&:to_i)
puts !((!a.nonzero? && !b.nonzero?) || !c.nonzero?) ^ d.nonzero? ? 1 : 0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?