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?

paizaラーニングレベルアップ問題集の「半加算器」「全加算器」をやってみた。

Posted at

paizaラーニングレベルアップ問題集の「半加算器」「全加算器」をやってみました。


問題
半加算器

全加算器


方針
半加算器
A B C S
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0

より、

  • $A=B=1$の場合のみ$C=1$
  • $A=B$の場合$S=0$、$A\neq B$の場合$S=1$

となるので、

  • $C=A\text{ and }B$
  • $S=A\text{ xor }B$

となります。(次の問題文の画像を見られれば分かられると思います)

全加算器
A2 B2 C1 A2+B2 C2 S
0 0 0 00 0 0
0 1 0 01 0 1
1 0 0 01 0 1
1 1 0 10 1 0
0 0 1 00 0 1
0 1 1 01 1 0
1 0 1 01 1 0
1 1 1 10 1 1

問題文の画像より
$(C_x, S_y)=(A\text{ and }B, A\text{ xor }B)$
$(C_y, S)=(S_y\text{ and }C_1, S_y\text{ xor }C_1)$
$C_2=C_x\text{ xor }C_y$ または $C_2=C_x\text{ or }C_y$
となります。一気に書くと
$C_2=(A\text{ and }B)\text{ xor }((A\text{ xor }B)\text{ and }C_1)$
または$C_2=(A\text{ and }B)\text{ or }((A\text{ xor }B)\text{ and }C_1)$
$S=A\text{ xor }B\text{ xor }C$ ※左から右に計算
となります。

$C_2$については

  • $A$と$B$の両方が$1$の場合
  • $A$と$B$の片方が$1$の場合、$C_1$が$1$の場合

のいずれかが成り立つ場合、$1$になるということです。両方とも成り立つことはないので、$\text{xor}$と$\text{or}$どちらでもよいということです。直感的には、$A,B,C_1$のうち2個以上が$1$ならば$C_2$が$1$になるでしょう。

$S$については、$A,B,C$の$1$の個数が奇数個なら$1$、偶数個なら$0$ということです。

C
半加算器
#include <stdio.h>

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

void half_adder(int a, int b, int *c, int *s) {
	*c = a & b;
	*s = a ^ b;
}

int main() {
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	int cx, sy, cy, s;
	half_adder(a, b, &cx, &sy);
	half_adder(sy, c, &cy, &s);
	printf("%d %d\n", cx | cy, s);
	return 0;
}

C++
半加算器
#include <iostream>
using namespace std;

int main() {
	int a, b;
	cin >> a >> b;
	cout << (a & b) << ' ' << (a ^ b) << endl;
	return 0;
}
全加算器
#include <iostream>
#include <utility>
using namespace std;

pair<int, int> half_adder(int a, int b) {
	return {a & b, a ^ b};
}

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	auto [cx, sy] = half_adder(a, b);
	auto [cy, s] = half_adder(sy, c);
	cout << (cx | cy) << ' ' << s << endl;
	return 0;
}
C#
半加算器
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) + ' ' + (a ^ b));
	}
}
全加算器
using System;

class Program
{
	private static int[] HalfAdder(int a, int b)
	{
		return new int[] { a & b, a ^ b };
	}
	
	public static void Main()
	{
		int[] abc = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int[] x = HalfAdder(abc[0], abc[1]);
		int[] y = HalfAdder(x[1], abc[2]);
		Console.WriteLine("" + (x[0] | y[0]) + ' ' + y[1]);
	}
}

Go
半加算器
package main
import "fmt"

func main() {
	var a, b int
	fmt.Scan(&a, &b)
	fmt.Println(a & b, a ^ b)
}
全加算器
package main
import "fmt"

func half_adder(a int, b int) (int, int) {
	return a & b, a ^ b
}

func main() {
	var a, b, c int
	fmt.Scan(&a, &b, &c)
	cx, sy := half_adder(a, b)
	cy, s := half_adder(sy, c)
	fmt.Println(cx | cy, s)
}

Java
半加算器
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) + ' ' + (a ^ b));
		sc.close();
	}
}
全加算器
import java.util.*;

public class Main {
	
	private static int[] halfAdder(int a, int b) {
		return new int[] { a & b, a ^ b };
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] x = halfAdder(sc.nextInt(), sc.nextInt());
		int[] y = halfAdder(x[1], sc.nextInt());
		System.out.println("" + (x[0] | x[1]) + ' ' + y[1]);
		sc.close();
	}
}

JavaScript
半加算器
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(a & b, a ^ b);
全加算器
function halfAdder(a, b) {
	return [a & b, a ^ b]	
}

const [a, b, c] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
const [cx, sy] = halfAdder(a, b);
const [cy, s] = halfAdder(sy, c);
console.log(cx | cy, s);

Kotlin
半加算器
fun main() {
	val (a, b) = readLine()!!.split(' ').map { it.toInt() }
	println("" + (a and b) + ' ' + (a xor b))
}
全加算器
fun halfAdder(a: Int, b: Int): Pair<Int, Int> {
	return Pair(a and b, a xor b)
}

fun main() {
	val (a, b, c) = readLine()!!.split(' ').map { it.toInt() }
	val (cx, sy) = halfAdder(a, b)
	val (cy, s) = halfAdder(sy, c)
	println("" + (cx or cy) + ' ' + s)
}

PHP
半加算器
<?php
	[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
	echo $a & $b, ' ', $a ^ $b, PHP_EOL;
?>
全加算器
<?php
	function half_adder($a, $b) {
		return [$a & $b, $a ^ $b];
	}
	
	[$a, $b, $c] = array_map("intval", explode(' ', fgets(STDIN)));
	[$cx, $sy] = half_adder($a, $b);
	[$cy, $s] = half_adder($sy, $c);
	echo $cx | $cy, ' ', $s, PHP_EOL;
?>

Perl
半加算器
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
print $a & $b, ' ', $a ^ $b, $/;
全加算器
sub half_adder {
	return ($_[0] & $_[1], $_[0] ^ $_[1]);
}

my ($a, $b, $c) = map { int($_) } split ' ', <STDIN>;
my ($cx, $sy) = half_adder($a, $b);
my ($cy, $s) = half_adder($sy, $c);
print $cx | $cy, ' ', $s, $/;

Python3
半加算器
a, b = map(int, input().split())
print(a & b, a ^ b)
全加算器
def half_adder(a, b):
	return a & b, a ^ b


a, b, c = map(int, input().split())
cx, sy = half_adder(a, b)
cy, s = half_adder(sy, c)
print(cx | cy, s)

Ruby
半加算器
a, b = gets.split.map(&:to_i)
print a & b, ' ', a ^ b, "\n"
全加算器
def half_adder(a, b)
	return a & b, a ^ b
end

a, b, c = gets.split.map(&:to_i)
cx, sy = half_adder(a, b)
cy, s = half_adder(sy, c)
print cx | cy, ' ', s, "\n"

Scala
半加算器
import scala.io.StdIn._

object Main extends App{
	val Array(a, b) = readLine().split(' ').map { _.toInt }
	println("" + (a & b) + ' ' + (a ^ b))
}
全加算器
import scala.io.StdIn._

object Main extends App{
	def halfAdder(a: Int, b: Int): (Int, Int) = {
		(a & b, a ^ b)
	}
	
	val Array(a, b, c) = readLine().split(' ').map { _.toInt }
	val (cx, sy) = halfAdder(a, b)
	val (cy, s) = halfAdder(sy, c)
	println("" + (cx | cy) + " " + s)
}

Swift
半加算器
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b) = (ab[0], ab[1])
print(a & b, a ^ b)
全加算器
func halfAdder(a: Int, b: Int) -> (Int, Int) {
	return (a & b, a ^ b)
}

let abc = readLine()!.split(separator: " ").compactMap { Int($0) }
let (cx, sy) = halfAdder(a: abc[0], b: abc[1])
let (cy, s) = halfAdder(a: sy, b: abc[2])
print(cx | cy, s)
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?