Notice
Recent Posts
Recent Comments
Link
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준-10757번/Java] 큰 수 A + B 본문

Algorithm

[백준-10757번/Java] 큰 수 A + B

1984 2022. 11. 10. 16:11

* 문자열로 처리

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		StringBuffer sb1 = new StringBuffer(sc.next());
		StringBuffer sb2 = new StringBuffer(sc.next());

		String str1 = sb1.reverse().toString();
		String str2 = sb2.reverse().toString();

		char[] num1 = str1.toCharArray();
		char[] num2 = str2.toCharArray();

		if (num1.length <= num2.length) {
			num1 = str2.toCharArray();
			num2 = str1.toCharArray();
		}

		int post_plus = 0;
		String answer = "";
		for (int i = 0; i < num1.length; i++) {
			if (i < num2.length) {
				int i1 = Integer.parseInt(Character.toString(num1[i]));
				int i2 = Integer.parseInt(Character.toString(num2[i]));
				answer += (i1 + i2 + post_plus) % 10;
				post_plus = (i1 + i2 + post_plus) / 10;
			} else {
				int i1 = Integer.parseInt(Character.toString(num1[i]));
				answer += (i1 + post_plus) % 10;
				post_plus = (i1 + post_plus) / 10;

			}
		}
		if (post_plus != 0) {
			answer += post_plus;
		}

		StringBuffer sb_answer = new StringBuffer(answer);

		System.out.println(sb_answer.reverse());
		sc.close();
	}

}
728x90

'Algorithm' 카테고리의 다른 글

[백준-3052번/Java] 나머지  (0) 2022.11.10
[백준-15552번/Java] 빠른 A+B  (0) 2022.11.10
[백준-2292번/Java] 벌집  (0) 2022.11.10
[백준-1712번/Java] 손익분기점  (0) 2022.11.10
[백준-1065번/Java] 한수  (0) 2022.11.10
Comments