paizaラーニングレベルアップ問題集の【論理演算メニュー】をやってみました。
まずは「基本」と名の付く7問です。
問題
論理積(AND)の基本
論理和(OR)の基本
否定(NOT)の基本
排他的論理和(XOR)の基本
NAND演算の基本
NOR演算の基本
XNOR演算の基本
方針
今回の入力値は0, 1の二値なので
- ビット演算子(オペランド及び返却値が整数型)を用いる場合
- 論理演算子(オペランド及び返却値が真偽値型)を用いる場合
でやってみます。
否定(NOT)の基本
今回は
- ビット反転させて、1との論理積を取ることにより最下位ビットを取得する
方式を採用していますが、他に
- 1との排他的論理和を取る
- 1から引く(公式解説)
等の方式があります。
排他的論理和(XOR)の基本
論理演算子においては、比較演算子!=を用いています。
NXORの基本
a==bでも答えなのですが、今回は「排他的論理和の否定を取る」ことにしています。
C
論理積(AND)の基本
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a & b);
return 0;
}
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a && b);
return 0;
}
論理和(OR)の基本
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a | b);
return 0;
}
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a || b);
return 0;
}
否定(NOT)の基本
#include <stdio.h>
int main() {
int a;
scanf("%d", &a);
printf("%d\n", ~a & 1);
return 0;
}
#include <stdio.h>
int main() {
int a;
scanf("%d", &a);
printf("%d\n", !a);
return 0;
}
排他的論理和(XOR)の基本
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a ^ b);
return 0;
}
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a != b);
return 0;
}
NAND演算の基本
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", ~(a & b) & 1);
return 0;
}
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", !(a && b));
return 0;
}
NOR演算の基本
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", ~(a | b) & 1);
return 0;
}
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", !(a || b));
return 0;
}
XNOR演算の基本
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", ~(a ^ b) & 1);
return 0;
}
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", !(a != b));
return 0;
}
C++
論理積(AND)の基本
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << (a & b) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << (a && b) << endl;
return 0;
}
論理和(OR)の基本
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << (a | b) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << (a || b) << endl;
return 0;
}
否定(NOT)の基本
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
cout << (~a & 1) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
cout << !a << endl;
return 0;
}
排他的論理和(XOR)の基本
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << (a ^ b) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << (a != b) << endl;
return 0;
}
NAND演算の基本
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << (~(a & b) & 1) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << !(a && b) << endl;
return 0;
}
NOR演算の基本
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << (~(a | b) & 1) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << !(a || b) << endl;
return 0;
}
XNOR演算の基本
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << (~(a ^ b) & 1) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << !(a != b) << endl;
return 0;
}
C#
論理積(AND)の基本
using System;
class Program
{
public static void Main()
{
int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int a = ab[0];
int b = ab[1];
Console.WriteLine(a & b);
}
}
using System;
class Program
{
public static void Main()
{
bool[] ab = Array.ConvertAll(Console.ReadLine().Split(), x => int.Parse(x) != 0);
bool a = ab[0];
bool b = ab[1];
Console.WriteLine(a && b ? 1 : 0);
}
}
論理和(OR)の基本
using System;
class Program
{
public static void Main()
{
int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int a = ab[0];
int b = ab[1];
Console.WriteLine(a | b);
}
}
using System;
class Program
{
public static void Main()
{
bool[] ab = Array.ConvertAll(Console.ReadLine().Split(), x => int.Parse(x) != 0);
bool a = ab[0];
bool b = ab[1];
Console.WriteLine(a || b ? 1 : 0);
}
}
否定(NOT)の基本
using System;
class Program
{
public static void Main()
{
int a = int.Parse(Console.ReadLine());
Console.WriteLine(~a & 1);
}
}
using System;
class Program
{
public static void Main()
{
bool a = int.Parse(Console.ReadLine()) != 0;
Console.WriteLine(!a ? 1 : 0);
}
}
排他的論理和(XOR)の基本
using System;
class Program
{
public static void Main()
{
int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int a = ab[0];
int b = ab[1];
Console.WriteLine(a ^ b);
}
}
using System;
class Program
{
public static void Main()
{
bool[] ab = Array.ConvertAll(Console.ReadLine().Split(), x => int.Parse(x) != 0);
bool a = ab[0];
bool b = ab[1];
Console.WriteLine(a != b ? 1 : 0);
}
}
NAND演算の基本
using System;
class Program
{
public static void Main()
{
int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int a = ab[0];
int b = ab[1];
Console.WriteLine(~(a & b) & 1);
}
}
using System;
class Program
{
public static void Main()
{
bool[] ab = Array.ConvertAll(Console.ReadLine().Split(), x => int.Parse(x) != 0);
bool a = ab[0];
bool b = ab[1];
Console.WriteLine(!(a && b) ? 1 : 0);
}
}
NOR演算の基本
using System;
class Program
{
public static void Main()
{
int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int a = ab[0];
int b = ab[1];
Console.WriteLine(~(a | b) & 1);
}
}
using System;
class Program
{
public static void Main()
{
bool[] ab = Array.ConvertAll(Console.ReadLine().Split(), x => int.Parse(x) != 0);
bool a = ab[0];
bool b = ab[1];
Console.WriteLine(!(a || b) ? 1 : 0);
}
}
XNOR演算の基本
using System;
class Program
{
public static void Main()
{
int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int a = ab[0];
int b = ab[1];
Console.WriteLine(~(a ^ b) & 1);
}
}
using System;
class Program
{
public static void Main()
{
bool[] ab = Array.ConvertAll(Console.ReadLine().Split(), x => int.Parse(x) != 0);
bool a = ab[0];
bool b = ab[1];
Console.WriteLine(!(a != b) ? 1 : 0);
}
}
Go
論理積(AND)の基本
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
fmt.Println(a & b)
}
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
if (a != 0) && (b != 0) {
fmt.Println(1)
} else {
fmt.Println(0)
}
}
論理和(OR)の基本
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
fmt.Println(a | b)
}
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
if (a != 0) || (b != 0) {
fmt.Println(1)
} else {
fmt.Println(0)
}
}
否定(NOT)の基本
package main
import "fmt"
func main() {
var a int
fmt.Scan(&a)
fmt.Println(^a & 1)
}
package main
import "fmt"
func main() {
var a int
fmt.Scan(&a)
if !(a != 0) {
fmt.Println(1)
} else {
fmt.Println(0)
}
}
排他的論理和(XOR)の基本
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
fmt.Println(a ^ b)
}
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
if (a != 0) != (b != 0) {
fmt.Println(1)
} else {
fmt.Println(0)
}
}
NAND演算の基本
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
fmt.Println(^(a & b) & 1)
}
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
if !((a != 0) && (b != 0)) {
fmt.Println(1)
} else {
fmt.Println(0)
}
}
NOR演算の基本
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
fmt.Println(^(a | b) & 1)
}
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
if !((a != 0) || (b != 0)) {
fmt.Println(1)
} else {
fmt.Println(0)
}
}
XNOR演算の基本
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
fmt.Println(^(a ^ b) & 1)
}
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
if !((a != 0) != (b != 0)) {
fmt.Println(1)
} else {
fmt.Println(0)
}
}
Java
論理積(AND)の基本
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a & b);
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;
System.out.println(a && b ? 1 : 0);
sc.close();
}
}
論理和(OR)の基本
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a | b);
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;
System.out.println(a || b ? 1 : 0);
sc.close();
}
}
否定(NOT)の基本
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println(~a & 1);
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;
System.out.println(!a ? 1 : 0);
sc.close();
}
}
排他的論理和(XOR)の基本
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a ^ b);
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;
System.out.println(a != b ? 1 : 0);
sc.close();
}
}
NAND演算の基本
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(~(a & b) & 1);
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;
System.out.println(!(a && b) ? 1 : 0);
sc.close();
}
}
NOR演算の基本
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(~(a | b) & 1);
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;
System.out.println(!(a || b) ? 1 : 0);
sc.close();
}
}
XNOR演算の基本
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(~(a ^ b) & 1);
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;
System.out.println(!(a != b) ? 1 : 0);
sc.close();
}
}
JavaScript
論理積(AND)の基本
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(a & b);
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(a && b ? 1 : 0);
論理和(OR)の基本
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(a | b);
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(a || b ? 1 : 0);
否定(NOT)の基本
const a = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
console.log(~a & 1);
const a = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
console.log(!a ? 1 : 0);
排他的論理和(XOR)の基本
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(a ^ b);
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(a != b ? 1 : 0);
NAND演算の基本
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(~(a & b) & 1);
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(!(a && b) ? 1 : 0);
NOR演算の基本
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(~(a | b) & 1);
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(!(a || b) ? 1 : 0);
XNOR演算の基本
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(~(a ^ b) & 1);
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(!(a != b) ? 1 : 0);
Kotlin
論理積(AND)の基本
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() }
println(a & b)
}
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() != 0 }
println(if (a && b) 1 else 0)
}
論理和(OR)の基本
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() }
println(a or b)
}
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() != 0 }
println(if (a || b) 1 else 0)
}
XNOR演算の基本
import java.util.*
fun main() {
val sc = Scanner(System.`in`)
val a = sc.nextInt()
sc.close()
println(a.inv() and 1)
}
import java.util.*
fun main() {
val sc = Scanner(System.`in`)
val a = sc.nextInt() != 0
sc.close()
println(if (!a) 1 else 0)
}
排他的論理和(XOR)の基本
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() }
println(a ^ b)
}
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() != 0 }
println(if (a != b) 1 else 0)
}
NAND演算の基本
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() }
println((a and b).inv() and 1)
}
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() != 0 }
println(if (!(a && b)) 1 else 0)
}
NOR演算の基本
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() }
println((a or b).inv() and 1)
}
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() != 0 }
println(if (!(a || b)) 1 else 0)
}
XNOR演算の基本
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() }
println((a xor b).inv() and 1)
}
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() != 0 }
println(if (!(a != b)) 1 else 0)
}
PHP
論理積(AND)の基本
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo $a & $b, PHP_EOL;
?>
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo $a && $b ? 1 : 0, PHP_EOL;
?>
論理和(OR)の基本
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo $a | $b, PHP_EOL;
?>
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo $a || $b ? 1 : 0, PHP_EOL;
?>
否定(NOT)の基本
<?php
$a = intval(fgets(STDIN));
echo ~$a & 1, PHP_EOL;
?>
<?php
$a = intval(fgets(STDIN));
echo !$a ? 1 : 0, PHP_EOL;
?>
排他的論理和(XOR)の基本
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo $a ^ $b, PHP_EOL;
?>
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo $a != $b ? 1 : 0, PHP_EOL;
?>
NAND演算の基本
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo ~($a & $b) & 1, PHP_EOL;
?>
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo !($a && $b) ? 1 : 0, PHP_EOL;
?>
NOR演算の基本
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo ~($a | $b) & 1, PHP_EOL;
?>
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo !($a || $b) ? 1 : 0, PHP_EOL;
?>
XNOR演算の基本
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo ~($a ^ $b) & 1, PHP_EOL;
?>
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
echo !($a != $b) ? 1 : 0, PHP_EOL;
?>
Perl
論理積(AND)の基本
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print $a & $b, $/;
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print $a && $b ? 1 : 0, $/;
論理和(OR)の基本
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print $a | $b, $/;
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print $a || $b ? 1 : 0, $/;
否定(NOT)の基本
my $a = int(<STDIN>);
print ~$a & 1, $/;
my $a = int(<STDIN>);
print !$a ? 1 : 0, $/;
排他的論理和(XOR)の基本
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print $a ^ $b, $/;
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print $a != $b ? 1 : 0, $/;
NAND演算の基本
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print ~($a & $b) & 1, $/;
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print !($a && $b) ? 1 : 0, $/;
NOR演算の基本
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print ~($a | $b) & 1, $/;
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print !($a || $b) ? 1 : 0, $/;
XNOR演算の基本
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print ~($a ^ $b) & 1, $/;
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print !($a != $b) ? 1 : 0, $/;
Python3
論理積(AND)の基本
a, b = map(int, input().split())
print(a & b)
a, b = map(int, input().split())
print(a and b)
論理和(OR)の基本
a, b = map(int, input().split())
print(a | b)
a, b = map(int, input().split())
print(a or b)
否定(NOT)の基本
a = int(input())
print(~a & 1)
a = int(input())
print(int(not a))
排他的論理和(XOR)の基本
a, b = map(int, input().split())
print(a ^ b)
a, b = map(int, input().split())
print(int(a != b))
NAND演算の基本
a, b = map(int, input().split())
print(~(a & b) & 1)
a, b = map(int, input().split())
print(int(not (a and b)))
NOR演算の基本
a, b = map(int, input().split())
print(~(a | b) & 1)
a, b = map(int, input().split())
print(int(not (a or b)))
XNOR演算の基本
a, b = map(int, input().split())
print(~(a ^ b) & 1)
a, b = map(int, input().split())
print(int(not a != b))
Ruby
論理積(AND)の基本
a, b = gets.split.map(&:to_i)
p a & b
a, b = gets.split.map { |x| x.to_i != 0 }
p a && b ? 1 : 0
論理和(OR)の基本
a, b = gets.split.map(&:to_i)
p a | b
a, b = gets.split.map { |x| x.to_i != 0 }
p a || b ? 1 : 0
否定(NOT)の基本
a = gets.to_i
p ~a & 1
a = gets.to_i != 0
p !a ? 1 : 0
排他的論理和(XOR)の基本
a, b = gets.split.map(&:to_i)
p a ^ b
a, b = gets.split.map { |x| x.to_i != 0 }
p a != b ? 1 : 0
NAND演算の基本
a, b = gets.split.map(&:to_i)
p ~(a & b) & 1
a, b = gets.split.map { |x| x.to_i != 0 }
p !(a && b) ? 1 : 0
NOR演算の基本
a, b = gets.split.map(&:to_i)
p ~(a | b) & 1
a, b = gets.split.map { |x| x.to_i != 0 }
p !(a || b) ? 1 : 0
XNOR演算の基本
a, b = gets.split.map(&:to_i)
p ~(a ^ b) & 1
a, b = gets.split.map { |x| x.to_i != 0 }
p !(a != b) ? 1 : 0
Scala
論理積(AND)の基本
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt }
println(a & b)
}
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt != 0 }
println(if (a && b) 1 else 0)
}
論理和(OR)の基本
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt }
println(a | b)
}
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt != 0 }
println(if (a || b) 1 else 0)
}
否定(NOT)の基本
import java.util._
object Main extends App{
val sc = new Scanner(System.in)
val a = sc.nextInt()
sc.close()
println(~a & 1)
}
import java.util._
object Main extends App{
val sc = new Scanner(System.in)
val a = sc.nextInt() != 0
sc.close()
println(if (!a) 1 else 0)
}
排他的論理和(XOR)の基本
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt }
println(a ^ b)
}
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt != 0 }
println(if (a != b) 1 else 0)
}
NAND演算の基本
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt }
println(~(a & b) & 1)
}
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt != 0 }
println(if (!(a && b)) 1 else 0)
}
NOR演算の基本
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt }
println(~(a | b) & 1)
}
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt != 0 }
println(if (!(a || b)) 1 else 0)
}
XNOR演算の基本
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt }
println(~(a ^ b) & 1)
}
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt != 0 }
println(if (!(a != b)) 1 else 0)
}
Swift
論理積(AND)の基本
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b) = (ab[0], ab[1])
print(a & b)
let ab = readLine()!.split(separator: " ").compactMap { Int($0) != 0 }
let (a, b) = (ab[0], ab[1])
print(a && b ? 1 : 0)
論理和(OR)の基本
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b) = (ab[0], ab[1])
print(a | b)
let ab = readLine()!.split(separator: " ").compactMap { Int($0) != 0 }
let (a, b) = (ab[0], ab[1])
print(a || b ? 1 : 0)
否定(NOT)の基本
let a = Int(readLine()!)!
print(~a & 1)
let a = Int(readLine()!)! != 0
print(!a ? 1 : 0)
排他的論理和(XOR)の基本
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b) = (ab[0], ab[1])
print(a ^ b)
let ab = readLine()!.split(separator: " ").compactMap { Int($0) != 0 }
let (a, b) = (ab[0], ab[1])
print(a != b ? 1 : 0)
NAND演算の基本
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b) = (ab[0], ab[1])
print(~(a & b) & 1)
let ab = readLine()!.split(separator: " ").compactMap { Int($0) != 0 }
let (a, b) = (ab[0], ab[1])
print(!(a && b) ? 1 : 0)
NOR演算の基本
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b) = (ab[0], ab[1])
print(~(a | b) & 1)
let ab = readLine()!.split(separator: " ").compactMap { Int($0) != 0 }
let (a, b) = (ab[0], ab[1])
print(!(a || b) ? 1 : 0)
XNOR演算の基本
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b) = (ab[0], ab[1])
print(~(a ^ b) & 1)
let ab = readLine()!.split(separator: " ").compactMap { Int($0) != 0 }
let (a, b) = (ab[0], ab[1])
print(!(a != b) ? 1 : 0)