結果

問題 No.353 ヘイトプラス
ユーザー spaciaspacia
提出日時 2016-06-02 14:22:13
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,685 bytes
コンパイル時間 2,295 ms
コンパイル使用メモリ 77,836 KB
実行使用メモリ 41,820 KB
最終ジャッジ日時 2024-04-16 23:03:24
合計ジャッジ時間 5,405 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

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(add(a, b));

		sc.close();

	}

	// a + bを計算する
	public static int add (int a, int b) {

		// a+b(2進数)
		String a_plus_b_bin = "";

		// aとbをそれぞれ2進数に
		String a_bin = Integer.toBinaryString(a);
		String b_bin = Integer.toBinaryString(b);

		// 足し算した際の繰り上がりの有無
		boolean is_move_up = false;

		// 文字列の長さを合わせる
		while (a_bin.length() < b_bin.length()) a_bin = "0".concat(a_bin);
		while (a_bin.length() > b_bin.length()) b_bin = "0".concat(b_bin);

		for (int i = a_bin.length() - 1; i >= 0; i--) {
			a_plus_b_bin = aPlusBBinSingleDigit(a_bin.charAt(i), b_bin.charAt(i), is_move_up).concat(a_plus_b_bin);
			is_move_up = isNextMoveUp(a_bin.charAt(i), b_bin.charAt(i), is_move_up);
		}

		// 最後に繰り上がりがあったら1を付加
		if (is_move_up) a_plus_b_bin = "1".concat(a_plus_b_bin);

		return Integer.parseInt(a_plus_b_bin, 2);

	}

	// 2進数aとbの1桁単位の足し算を行い、1の位を求める
	public static String aPlusBBinSingleDigit (char a, char b, boolean is_move_up) {
		if ((a == '1') ^ (b == '1') ^ is_move_up)
			return "1";
		else
			return "0";
	}

	// 2進数aとbの1桁単位の足し算を行い、繰り上がりが起きるかを判定
	public static boolean isNextMoveUp(char a, char b, boolean is_move_up) {
		int[] bins = {a - '0', b - '0', is_move_up ? 1 : 0};
		Arrays.sort(bins);
		return bins[1] == 1 && bins[2] == 1;
	}

}
0