Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class APlusB {
- static String stringSum(String str1, String str2) {
- if (str1.length() > str2.length()) {
- String t = str1;
- str1 = str2;
- str2 = t;
- }
- String str = "";
- int n1 = str1.length(), n2 = str2.length();
- str1 = new StringBuilder(str1).reverse().toString();
- str2 = new StringBuilder(str2).reverse().toString();
- int carry = 0;
- for (int i = 0; i < n1; i++) {
- int sum = ((int) (str1.charAt(i) - '0') + (int) (str2.charAt(i) - '0') + carry);
- str += (char) (sum % 10 + '0');
- carry = sum / 10;
- }
- for (int i = n1; i < n2; i++) {
- int sum = ((int) (str2.charAt(i) - '0') + carry);
- str += (char) (sum % 10 + '0');
- carry = sum / 10;
- }
- if (carry > 0)
- str += (char) (carry + '0');
- str = new StringBuilder(str).reverse().toString();
- return str;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int N = sc.nextInt();
- for (int i = 0; i < N; i++) {
- String a = sc.next();
- String b = sc.next();
- System.out.println(stringSum(a, b));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement