1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paizaラーニングの論理演算メニューのrubyの解答例

Posted at

Paizaラーニングの論理演算メニューのrubyでの模範解答です。
答えだけですみません。
 
STEP: 1 論理積( AND )の基本
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step1

step1.rb
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

step2.rb
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

step3.rb
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

step4.rb
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

step5.rb
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

step6.rb
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

step7.rb
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

step8.rb
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

step9.rb
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

step10.rb
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
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?