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

Envío 2937

Problema 0x31 - Sumarle un dígito a número muy grande

  • Autor: hamorillo
  • Fecha: 2021-02-12 14:03:34 UTC (Hace casi 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.144 s 16 KBi
#2
Correcto
0.194 s 12 KBi
#3
Correcto
0.153 s 15 KBi
#4
Correcto
0.146 s 16 KBi
#5
Correcto
0.177 s 16 KBi
#6
Correcto
0.191 s 12 KBi
#7
Correcto
0.164 s 15 KBi
#8
Correcto
0.157 s 16 KBi
#9
Correcto
0.169 s 16 KBi
#10
Correcto
0.166 s 15 KBi
#11
Correcto
0.193 s 12 KBi
#12
Correcto
0.199 s 12 KBi
#13
Correcto
0.153 s 16 KBi
#14
Correcto
0.171 s 16 KBi
#15
Correcto
0.198 s 12 KBi
#16
Correcto
0.175 s 16 KBi
#17
Correcto
0.163 s 16 KBi
#18
Correcto
0.166 s 16 KBi
#19
Correcto
0.154 s 15 KBi
#20
Correcto
0.16 s 16 KBi
#21
Correcto
0.163 s 16 KBi
#22
Correcto
0.162 s 16 KBi
#23
Correcto
0.165 s 16 KBi
#24
Correcto
0.171 s 16 KBi
#25
Correcto
0.182 s 12 KBi
#26
Correcto
0.187 s 12 KBi
#27
Correcto
0.143 s 16 KBi
#28
Correcto
0.163 s 16 KBi
#29
Correcto
0.158 s 16 KBi
#30
Correcto
0.166 s 16 KBi
#31
Correcto
0.147 s 16 KBi
#32
Correcto
0.175 s 16 KBi
#33
Correcto
0.172 s 16 KBi
#34
Correcto
0.166 s 15 KBi
#35
Correcto
0.164 s 16 KBi
#36
Correcto
0.415 s 15 KBi
#37
Correcto
0.413 s 21 KBi
#38
Correcto
0.412 s 23 KBi
#39
Correcto
0.415 s 23 KBi
#40
Correcto
0.386 s 22 KBi
#41
Correcto
0.444 s 24 KBi
#42
Correcto
0.382 s 20 KBi
Puntos totales: 100 / 100

Código

import java.util.Arrays;
import java.util.Scanner;

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

        int carry = data.addingNumber;
        for (int index = data.number.length - 1; index >= 0; index--) {
            int sum = data.number[index] + carry;
            data.number[index] = (char) (sum % 10);
            carry = sum / 10;

            if (carry == 0) {
                break;
            }
        }

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

        printArray(data.number);
    }

    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);

        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)));
        }

        data.number = numbers;
        data.addingNumber = in.nextInt();
        return data;
    }

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