paizaラーニングレベルアップ問題集の「論理演算を用いた計算のまとめ」をやってみた。

paizaラーニングレベルアップ問題集の[論理演算を用いた計算のまとめ](https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_boss)をやってみました。
- - -
###### 問題
https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_boss
- - -
###### 方針
問題文どおりに書けば
```math
\text{not }\left(\left(\text{not }A\text{ and not }B\right)\text{ or not }C\right)\text{ xor }D
```
ですが、[ド・モルガンの法則](https://ja.wikipedia.org/wiki/%E3%83%89%E3%83%BB%E3%83%A2%E3%83%AB%E3%82%AC%E3%83%B3%E3%81%AE%E6%B3%95%E5%89%87)を用いると
```math
\begin{eqnarray}
&&\text{not }\left(\left(\text{not }A\text{ and not }B\right)\text{ or not }C\right)\text{ xor }D\\
=&&\text{not }\left(\text{not }\left(A\text{ or }B\right)\text{ or not }C\right)\text{ xor }D\\
=&&\text{not }\left(\text{not }\left(\left(A\text{ or }B\right)\text{ and }C\right)\right)\text{ xor }D\\
=&&\left(\left(A\text{ or }B\right)\text{ and }C\right)\text{ xor }D\\
\end{eqnarray}
```
となります。
勿論、元の式を実装してもいいのですが、今回はド・モルガンの法則を適用した後の式で実装します。
- - -
###### C
```c
#include <stdio.h>

int main() {
	int a, b, c, d;
	scanf("%d %d %d %d", &a, &b, &c, &d);
	printf("%d\n", ((a | b) & c) ^ d);
	return 0;
}
```
```c
#include <stdio.h>

int main() {
	int a, b, c, d;
	scanf("%d %d %d %d", &a, &b, &c, &d);
	printf("%d\n", ((a || b) && c) != d);
	return 0;
}
```
- - -
###### C++
```cpp
#include <iostream>
using namespace std;

int main() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	cout << (((a | b) & c) ^ d) << endl;
	return 0;
}
```
```cpp
#include <iostream>
using namespace std;

int main() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	cout << (((a || b) && c) != d) << endl;
	return 0;
}
```
- - -
###### C#
```cs
using System;

class Program
{
	public static void Main()
	{
		int[] abcd = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int a = abcd[0], b = abcd[1], c = abcd[2], d = abcd[3];
		Console.WriteLine(((a | b) & c) ^ d);
	}
}
```
```cs
using System;

class Program
{
	public static void Main()
	{
		bool[] abcd = Array.ConvertAll(Console.ReadLine().Split(), x => int.Parse(x) != 0);
		bool a = abcd[0], b = abcd[1], c = abcd[2], d = abcd[3];
		Console.WriteLine(((a || b) && c) != d ? 1 : 0);
	}
}
```
- - -
###### Go
```go
package main
import "fmt"

func main() {
	var a, b, c, d int
	fmt.Scan(&a, &b, &c, &d)
	fmt.Println(((a | b) & c) ^ d)
}
```
```go
package main
import "fmt"

func main() {
	var a, b, c, d int
	fmt.Scan(&a, &b, &c, &d)
	if (((a != 0) || (b != 0)) && (c != 0)) != (d != 0) {
		fmt.Println(1)
	} else {
		fmt.Println(0)
	}
}
```
- - -
###### Java
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(((sc.nextInt() | sc.nextInt()) & sc.nextInt()) ^ sc.nextInt());
		sc.close();
	}
}
```
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		boolean a = sc.nextInt() != 0;
		boolean b = sc.nextInt() != 0;
		boolean c = sc.nextInt() != 0;
		boolean d = sc.nextInt() != 0;
		System.out.println(((a | b) & c) ^ d ? 1 : 0);
		sc.close();
	}
}
```
- - -
###### JavaScript
```js
const [a, b, c, d] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split(' ').map(Number);
console.log(((a | b) & c) ^ d);
```
```js
const [a, b, c, d] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split(' ').map(Number);
console.log(((a || b) && c) != d ? 1 : 0);
```
- - -
###### Kotlin
```kotlin
import java.util.*

fun main() {
	val sc = Scanner(System.`in`)
	println(((sc.nextInt() or sc.nextInt()) and sc.nextInt()) xor sc.nextInt())
	sc.close()
}
```
```kotlin
fun main() {
	val (a, b, c, d) = readLine()!!.split(' ').map { it.toInt() != 0 }
	println(if (((a || b) && c) != d) 1 else 0)
}
```
- - -
###### PHP
```php
<?php
	[$a, $b, $c, $d] = array_map("intval", explode(' ', fgets(STDIN)));
	echo (($a | $b) & $c) ^ $d, PHP_EOL;
?>
```
```php
<?php
	[$a, $b, $c, $d] = array_map("intval", explode(' ', fgets(STDIN)));
	echo (($a || $b) && $c) != $d ? 1 : 0, PHP_EOL;
?>
```
- - -
###### Perl
```pl
my ($a, $b, $c, $d) = map { int($_) } split ' ', <STDIN>;
print ((($a | $b) & $c) ^ $d . $/);
```
```pl
my ($a, $b, $c, $d) = map { int($_) } split ' ', <STDIN>;
print ((($a || $b) && $c) != $d ? 1 : 0 . $/);
```
- - -
###### Python3
```py
a, b, c, d = map(int, input().split())
print(((a | b) & c) ^ d)
```
```py
a, b, c, d = map(int, input().split())
print(int(((a or b) and c) != d))
```
- - -
###### Ruby
```rb
a, b, c, d = gets.split.map(&:to_i)
p ((a | b) & c) ^ d
```
```rb
a, b, c, d = gets.split.map { |x| x.to_i != 0 }
p ((a || b) && c) != d ? 1 : 0
```
- - -
###### Scala
```scala
import java.util._

object Main extends App{
	val sc = new Scanner(System.in)
	println(((sc.nextInt() | sc.nextInt()) & sc.nextInt()) ^ sc.nextInt())
	sc.close()
}
```
```scala
import scala.io.StdIn._

object Main extends App{
	val Array(a, b, c, d) = readLine().split(' ').map { _.toInt != 0 }
	println(if (((a || b) && c) != d) 1 else 0)
}
```
- - -
###### Swift
```swift
let abcd = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b, c, d) = (abcd[0], abcd[1], abcd[2], abcd[3])
print(((a | b) & c) ^ d)
```
```swift
let abcd = readLine()!.split(separator: " ").map { Int($0)! != 0 }
let (a, b, c, d) = (abcd[0], abcd[1], abcd[2], abcd[3])
print(((a || b) && c) != d ? 1 : 0)
```