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

Envío 2938

Problema 0x32 - Sumar dos números muy grandes

  • Autor: hamorillo
  • Fecha: 2021-02-12 14:21:01 UTC (Hace casi 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.183 s 12 KBi
#2
Correcto
0.178 s 12 KBi
#3
Correcto
0.162 s 15 KBi
#4
Correcto
0.171 s 16 KBi
#5
Correcto
0.149 s 16 KBi
#6
Correcto
0.173 s 16 KBi
#7
Correcto
0.137 s 16 KBi
#8
Correcto
0.167 s 15 KBi
#9
Correcto
0.147 s 16 KBi
#10
Correcto
0.181 s 16 KBi
#11
Correcto
0.176 s 12 KBi
#12
Correcto
0.176 s 12 KBi
#13
Correcto
0.147 s 16 KBi
#14
Correcto
0.195 s 12 KBi
#15
Correcto
0.179 s 12 KBi
#16
Correcto
0.154 s 15 KBi
#17
Correcto
0.161 s 16 KBi
#18
Correcto
0.162 s 15 KBi
#19
Correcto
0.148 s 16 KBi
#20
Correcto
0.149 s 15 KBi
#21
Correcto
0.145 s 15 KBi
#22
Correcto
0.147 s 16 KBi
#23
Correcto
0.153 s 16 KBi
#24
Correcto
0.19 s 15 KBi
#25
Correcto
0.147 s 16 KBi
#26
Correcto
0.151 s 16 KBi
#27
Correcto
0.155 s 16 KBi
#28
Correcto
0.152 s 16 KBi
#29
Correcto
0.438 s 20 KBi
#30
Correcto
0.489 s 26 KBi
#31
Correcto
0.437 s 16 KBi
#32
Correcto
0.407 s 24 KBi
#33
Correcto
0.468 s 16 KBi
#34
Correcto
0.405 s 23 KBi
#35
Correcto
0.411 s 23 KBi
#36
Correcto
0.391 s 22 KBi
#37
Correcto
0.416 s 23 KBi
#38
Correcto
0.39 s 20 KBi
Puntos totales: 100 / 100

Código

import java.util.Scanner;

import static java.lang.Math.max;

public class Main {
    public static void main(String[] args) {
        Data data = readWordFromStdin();

        int[] result = new int[max(data.number.length, data.number2.length)];
        int carry = 0;
        for (int index = 0; index < result.length; index++) {
            int sum = carry;
            if(index < data.number.length){
                sum += data.number[data.number.length - index - 1];
            }
            if(index < data.number2.length){
                sum += data.number2[data.number2.length - index - 1];
            }
            result[result.length - index - 1] = sum % 10;
            carry = sum / 10;
        }

        if (carry != 0) {
            int[] newArrayWithExtraCarry = new int[result.length + 1];
            newArrayWithExtraCarry[0] = carry;
            System.arraycopy(result, 0, newArrayWithExtraCarry, 1, newArrayWithExtraCarry.length - 1);
            result = newArrayWithExtraCarry;
        }

        printArray(result);
    }

    private static void printArray(int[] array) {
        for (int number : array) {
            System.out.print(number);
        }
    }

    private static Data readWordFromStdin() {
        Data data = new Data();
        Scanner in = new Scanner(System.in);

        data.number = readBigNumber(in);
        data.number2 = readBigNumber(in);
        return data;
    }

    private static int[] readBigNumber(Scanner in) {
        String line = in.nextLine();
        int[] numbers = new int[line.length()];
        for (int i = 0; i < line.length(); i++) {
            numbers[i] = Integer.valueOf(String.valueOf(line.charAt(i)));
        }
        return numbers;
    }

    private static class Data {
        int[] number;
        int[] number2;
    }
}