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

Envío 3356

Problema 0x32 - Sumar dos números muy grandes

  • Autor: datruq
  • Fecha: 2021-03-12 05:11:43 UTC (Hace casi 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.153 s 16 KBi
#2
Correcto
0.158 s 16 KBi
#3
Correcto
0.196 s 15 KBi
#4
Correcto
0.162 s 16 KBi
#5
Correcto
0.181 s 16 KBi
#6
Correcto
0.17 s 16 KBi
#7
Correcto
0.168 s 16 KBi
#8
Correcto
0.286 s 12 KBi
#9
Correcto
0.166 s 16 KBi
#10
Correcto
0.157 s 16 KBi
#11
Correcto
0.152 s 16 KBi
#12
Correcto
0.166 s 15 KBi
#13
Correcto
0.16 s 16 KBi
#14
Correcto
0.169 s 16 KBi
#15
Correcto
0.166 s 16 KBi
#16
Correcto
0.154 s 16 KBi
#17
Correcto
0.168 s 16 KBi
#18
Correcto
0.165 s 16 KBi
#19
Correcto
0.314 s 12 KBi
#20
Correcto
0.157 s 16 KBi
#21
Correcto
0.299 s 12 KBi
#22
Correcto
0.171 s 16 KBi
#23
Correcto
0.169 s 16 KBi
#24
Correcto
0.173 s 16 KBi
#25
Correcto
0.15 s 15 KBi
#26
Correcto
0.271 s 53 KBi
#27
Correcto
0.162 s 15 KBi
#28
Correcto
0.163 s 15 KBi
#29
Correcto
0.502 s 25 KBi
#30
Correcto
0.434 s 22 KBi
#31
Correcto
0.453 s 22 KBi
#32
Correcto
0.457 s 22 KBi
#33
Correcto
0.658 s 15 KBi
#34
Correcto
0.586 s 17 KBi
#35
Correcto
0.467 s 24 KBi
#36
Correcto
0.658 s 16 KBi
#37
Correcto
0.434 s 21 KBi
#38
Correcto
0.503 s 25 KBi
Puntos totales: 100 / 100

Código

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sn = new Scanner(System.in);
        String number = sn.nextLine();
        String number2 = sn.nextLine();
        List<Integer> longNumber = new ArrayList<>();
        List<Integer> longNumber2 = new ArrayList<>();
        for (int i = 0; i < number.length(); i++) {
            longNumber.add(Integer.parseInt(Character.toString(number.charAt(i))));
        }
        for (int i = 0; i < number2.length(); i++) {
            longNumber2.add(Integer.parseInt(Character.toString(number2.charAt(i))));
        }
        long index = longNumber.size();
        int carry = 0;
        List<Integer> upTo = longNumber2.size() > index ? longNumber2 : longNumber;
        Collections.reverse(longNumber);
        Collections.reverse(longNumber2);
        List<Integer> answer = new ArrayList<>();
        for (int i = 0; i < upTo.size(); i++) {
            int current = carry;
            if (i < longNumber.size()) {
                current += longNumber.get(i);
            }
            if (i < longNumber2.size()) {
                current += longNumber2.get(i);
            }
            answer.add(current % 10);
            carry = Math.round(current / 10);
        }

        if (carry > 0) {
            answer.add( 1);
        }
        Collections.reverse(answer);
        answer.forEach((a) -> System.out.print(a));
    }

}