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

Envío 7090

Problema 0x32 - Sumar dos números muy grandes

Caso # Resultado Tiempo Memoria
#1
Correcto
0.102 s 13 KBi
#2
Correcto
0.103 s 13 KBi
#3
Correcto
0.089 s 13 KBi
#4
Correcto
0.106 s 13 KBi
#5
Correcto
0.103 s 13 KBi
#6
Correcto
0.098 s 13 KBi
#7
Correcto
0.095 s 13 KBi
#8
Correcto
0.1 s 13 KBi
#9
Correcto
0.095 s 13 KBi
#10
Correcto
0.094 s 16 KBi
#11
Correcto
0.098 s 13 KBi
#12
Correcto
0.097 s 13 KBi
#13
Correcto
0.099 s 14 KBi
#14
Correcto
0.111 s 13 KBi
#15
Correcto
0.099 s 13 KBi
#16
Correcto
0.098 s 13 KBi
#17
Correcto
0.099 s 13 KBi
#18
Correcto
0.097 s 14 KBi
#19
Correcto
0.104 s 13 KBi
#20
Correcto
0.093 s 13 KBi
#21
Correcto
0.105 s 14 KBi
#22
Correcto
0.1 s 13 KBi
#23
Correcto
0.095 s 13 KBi
#24
Correcto
0.097 s 26 KBi
#25
Correcto
0.099 s 19 KBi
#26
Correcto
0.104 s 19 KBi
#27
Correcto
0.105 s 20 KBi
#28
Correcto
0.1 s 16 KBi
#29
Correcto
0.133 s 14 KBi
#30
Correcto
0.162 s 15 KBi
#31
Correcto
0.161 s 14 KBi
#32
Correcto
0.17 s 15 KBi
#33
Correcto
0.152 s 15 KBi
#34
Correcto
0.157 s 15 KBi
#35
Correcto
0.16 s 14 KBi
#36
Correcto
0.133 s 14 KBi
#37
Correcto
0.133 s 14 KBi
#38
Correcto
0.133 s 14 KBi
Puntos totales: 100 / 100

Código

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder num1 = new StringBuilder(scanner.nextLine()).reverse();
        StringBuilder num2 = new StringBuilder(scanner.nextLine()).reverse();
        int digit = 0;

        StringBuilder largerNum;
        StringBuilder shorterNum;
        if(num1.length() > num2.length()){
            largerNum = num1;
            shorterNum = num2;
        }else {
            largerNum = num2;
            shorterNum = num1;
        }

        StringBuilder result = new StringBuilder(largerNum);

        for (int i = 0; i < largerNum.length(); i++) {
            int sum;
            if(i < shorterNum.length()){
                sum = Character.getNumericValue(largerNum.charAt(i)) +
                        Character.getNumericValue(shorterNum.charAt(i)) + digit;
            }else{
                sum = Character.getNumericValue(largerNum.charAt(i)) + digit;
            }

            if(sum < 10){
                digit = 0;
                result.setCharAt(i, (char)(sum + '0'));
            }else{
                digit = 1;
                result.setCharAt(i, (char)((sum % 10) + '0'));
            }

            if((i == largerNum.length() - 1) && digit == 1){
                result.append('1');
            }
        }

        System.out.print(result.reverse());

        scanner.close();
    }
}