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?

More than 1 year has passed since last update.

Paiza Cランク獲得問題(Dランク相当) 論理演算メニュー

Posted at

paizaのCランク獲得問題の解答がなかったので、備忘録として残します。
明らかに簡単な問題の場合は省略しますが、個人的に少しでも考えたコードを残していきたいと思います。
初心者ですので醜いコードを書きますが、温かい目で見守っていただけると嬉しいです。
また、より良い記述方法などありましたら、コメント等で教えていただけると嬉しいです。

目次

  • 論理積( AND )の基本
  • 論理和( OR )の基本
  • 否定( NOT )の基本
  • NAND 演算の基本
  • 半加算器
  • 全加算器
  • 論理演算を用いた計算のまとめ

論理積( AND )の基本

問題文

using System;
class Program
{
    static void Main()
    {
        string []AB=Console.ReadLine().Split();
        int A=int.Parse(AB[0]);
        int B=int.Parse(AB[1]);
        int result=A & B;
        Console.WriteLine(result);
    }
}

論理和orについては、A | B
排他的論理和XORについては  A ^ B
で実装できます。

否定( NOT )の基本

問題文

using System;
class Program
{
    static void Main()
    {
        int A=int.Parse(Console.ReadLine());
        int result= 0;
        if(A==0){
            result=1;
        }
        
        Console.WriteLine(result);
    }
}

題意には沿っていません
## NAND 演算の基本
問題文

using System;
class Program
{
    static void Main()
    {
        string []AB=Console.ReadLine().Split();
        int A=int.Parse(AB[0]);
        int B=int.Parse(AB[1]);
        int result = (A & B) == 0 ? 1 : 0; 
        Console.WriteLine(result);
    }
}

否定もこれを元に出来たと思います。
NORは(A | B) == 0 ? 1 : 0;
XNORは(A ^ B) == 0 ? 1 : 0; で実装できます。

半加算器

問題文

using System;
class Program
{
    static void Main()
    {
        string []AB=Console.ReadLine().Split();
        int A=int.Parse(AB[0]);
        int B=int.Parse(AB[1]);
        int S = A ^ B;  
        int C = A & B;
        Console.WriteLine(C+" "+S);
    }
}

全加算器

問題文

using System;
class Program
{
    static void Main()
    {
        string []ABC1=Console.ReadLine().Split();
        int A=int.Parse(ABC1[0]);
        int B=int.Parse(ABC1[1]);
        int C1=int.Parse(ABC1[2]);
        int S = A ^ B ^ C1; 
        int C2 = (A & B) | (B & C1) | (C1 & A); 
        Console.WriteLine(C2+" "+S);
    }
}

題意に沿った形ではない気がします

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

問題文

using System;
class Program
{
    static void Main()
    {
        string []ABCD=Console.ReadLine().Split();
        int A=int.Parse(ABCD[0]);
        int B=int.Parse(ABCD[1]);
        int C=int.Parse(ABCD[2]);
        int D=int.Parse(ABCD[3]);
        int notE=A|B;
        int F=notE &C;
        int G=F^D;
        Console.WriteLine(G);
    }
}

ドモルガンを使いました。

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?