paizaラーニングレベルアップ問題集の論理演算を用いた計算のまとめをやってみました。
問題
方針
問題文どおりに書けば
\text{not }\left(\left(\text{not }A\text{ and not }B\right)\text{ or not }C\right)\text{ xor }D
ですが、ド・モルガンの法則を用いると
\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
#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;
}
#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 <iostream>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << (((a | b) & c) ^ d) << endl;
return 0;
}
#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#
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);
}
}
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
package main
import "fmt"
func main() {
var a, b, c, d int
fmt.Scan(&a, &b, &c, &d)
fmt.Println(((a | b) & c) ^ d)
}
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
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();
}
}
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
const [a, b, c, d] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split(' ').map(Number);
console.log(((a | b) & c) ^ d);
const [a, b, c, d] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split(' ').map(Number);
console.log(((a || b) && c) != d ? 1 : 0);
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()
}
fun main() {
val (a, b, c, d) = readLine()!!.split(' ').map { it.toInt() != 0 }
println(if (((a || b) && c) != d) 1 else 0)
}
PHP
<?php
[$a, $b, $c, $d] = array_map("intval", explode(' ', fgets(STDIN)));
echo (($a | $b) & $c) ^ $d, PHP_EOL;
?>
<?php
[$a, $b, $c, $d] = array_map("intval", explode(' ', fgets(STDIN)));
echo (($a || $b) && $c) != $d ? 1 : 0, PHP_EOL;
?>
Perl
my ($a, $b, $c, $d) = map { int($_) } split ' ', <STDIN>;
print ((($a | $b) & $c) ^ $d . $/);
my ($a, $b, $c, $d) = map { int($_) } split ' ', <STDIN>;
print ((($a || $b) && $c) != $d ? 1 : 0 . $/);
Python3
a, b, c, d = map(int, input().split())
print(((a | b) & c) ^ d)
a, b, c, d = map(int, input().split())
print(int(((a or b) and c) != d))
Ruby
a, b, c, d = gets.split.map(&:to_i)
p ((a | b) & c) ^ d
a, b, c, d = gets.split.map { |x| x.to_i != 0 }
p ((a || b) && c) != d ? 1 : 0
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()
}
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
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)
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)