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

Envío 2936

Problema 0x30 - Incrementar un número muy grande

  • Autor: hamorillo
  • Fecha: 2021-02-12 14:01:21 UTC (Hace casi 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.195 s 12 KBi
#2
Correcto
0.149 s 16 KBi
#3
Correcto
0.159 s 16 KBi
#4
Correcto
0.154 s 16 KBi
#5
Correcto
0.161 s 16 KBi
#6
Correcto
0.159 s 16 KBi
#7
Correcto
0.159 s 15 KBi
#8
Correcto
0.174 s 16 KBi
#9
Correcto
0.164 s 16 KBi
#10
Correcto
0.165 s 16 KBi
#11
Correcto
0.188 s 12 KBi
#12
Correcto
0.163 s 16 KBi
#13
Correcto
0.157 s 16 KBi
#14
Correcto
0.16 s 16 KBi
#15
Correcto
0.192 s 13 KBi
#16
Correcto
0.153 s 16 KBi
#17
Correcto
0.171 s 16 KBi
#18
Correcto
0.162 s 16 KBi
#19
Correcto
0.166 s 16 KBi
#20
Correcto
0.189 s 12 KBi
#21
Correcto
0.181 s 12 KBi
#22
Correcto
0.159 s 16 KBi
#23
Correcto
0.184 s 12 KBi
#24
Correcto
0.159 s 15 KBi
#25
Correcto
0.174 s 16 KBi
#26
Correcto
0.424 s 15 KBi
#27
Correcto
0.396 s 21 KBi
#28
Correcto
0.425 s 21 KBi
#29
Correcto
0.388 s 15 KBi
#30
Correcto
0.393 s 23 KBi
#31
Correcto
0.422 s 21 KBi
#32
Correcto
0.415 s 20 KBi
#33
Correcto
0.397 s 25 KBi
#34
Correcto
0.404 s 22 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 = 1;
        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;
        return data;
    }

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