█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 4638

Problema 0x32 - Sumar dos números muy grandes

  • Autor: 7yrionLannister
  • Fecha: 2021-08-05 00:17:03 UTC (Hace más de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.113 s 11 KBi
#2
Correcto
0.118 s 21 KBi
#3
Correcto
0.126 s 12 KBi
#4
Correcto
0.128 s 13 KBi
#5
Correcto
0.12 s 12 KBi
#6
Correcto
0.122 s 11 KBi
#7
Correcto
0.122 s 12 KBi
#8
Correcto
0.128 s 13 KBi
#9
Correcto
0.12 s 18 KBi
#10
Correcto
0.113 s 22 KBi
#11
Correcto
0.128 s 12 KBi
#12
Correcto
0.123 s 27 KBi
#13
Correcto
0.124 s 13 KBi
#14
Correcto
0.129 s 13 KBi
#15
Correcto
0.136 s 16 KBi
#16
Correcto
0.11 s 35 KBi
#17
Correcto
0.117 s 32 KBi
#18
Correcto
0.135 s 13 KBi
#19
Correcto
0.125 s 13 KBi
#20
Correcto
0.107 s 17 KBi
#21
Correcto
0.133 s 14 KBi
#22
Correcto
0.127 s 13 KBi
#23
Correcto
0.116 s 12 KBi
#24
Correcto
0.124 s 13 KBi
#25
Correcto
0.14 s 14 KBi
#26
Correcto
0.105 s 11 KBi
#27
Correcto
0.134 s 23 KBi
#28
Correcto
0.136 s 14 KBi
#29
Correcto
0.294 s 87 KBi
#30
Correcto
0.245 s 57 KBi
#31
Correcto
0.221 s 53 KBi
#32
Correcto
0.236 s 50 KBi
#33
Correcto
0.228 s 66 KBi
#34
Correcto
0.219 s 41 KBi
#35
Correcto
0.191 s 38 KBi
#36
Correcto
0.194 s 39 KBi
#37
Correcto
0.273 s 87 KBi
#38
Correcto
0.267 s 86 KBi
Puntos totales: 100 / 100

Código

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String num = br.readLine();
        String num2 = br.readLine();
        bw.write(add(num, num2) + "\n");
        br.close();
        bw.close();
    }

    public static String add(String num1, String num2) {
        int N1 = num1.length();
        int N2 = num2.length();
        boolean differentNumberOfDigits = N1 < N2;
        int N = Math.max(N1, N2);
        if (N1 > N2) {
            String temp = num1;
            num1 = num2;
            num2 = temp;
            differentNumberOfDigits = true;
        }
        if (differentNumberOfDigits) {
            int paddingZeros = Math.abs(N1 - N2);
            while (paddingZeros > 0) {
                num1 = '0' + num1;
                paddingZeros--;
            }
        }
        String result = "";
        N1 = N - 1;

        int carry = 0;
        for (int i = 0; i < N; i++) {
            int digit1 = num1.charAt(N1 - i) - '0';
            int digit2 = num2.charAt(N1 - i) - '0';
            int addition = digit1 + digit2 + carry;
            if (addition >= 10 && i < N1) {
                carry = 1;
                addition -= 10;
            } else {
                carry = 0;
            }
            result = addition + result;
        }

        return result;
    }
}