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

Envío 2935

Problema 0x30 - Incrementar un número muy grande

  • Autor: hamorillo
  • Fecha: 2021-02-12 14:00:25 UTC (Hace casi 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.152 s 16 KBi
#2
Correcto
0.156 s 15 KBi
#3
Correcto
0.162 s 16 KBi
#4
Correcto
0.163 s 16 KBi
#5
Correcto
0.141 s 16 KBi
#6
Correcto
0.194 s 20 KBi
#7
Correcto
0.15 s 15 KBi
#8
Correcto
0.188 s 16 KBi
#9
Correcto
0.154 s 16 KBi
#10
Correcto
0.152 s 16 KBi
#11
Correcto
0.194 s 21 KBi
#12
Correcto
0.163 s 16 KBi
#13
Correcto
0.151 s 15 KBi
#14
Correcto
0.15 s 16 KBi
#15
Correcto
0.198 s 18 KBi
#16
Correcto
0.163 s 15 KBi
#17
Correcto
0.158 s 16 KBi
#18
Correcto
0.164 s 16 KBi
#19
Correcto
0.142 s 16 KBi
#20
Correcto
0.168 s 16 KBi
#21
Correcto
0.167 s 15 KBi
#22
Correcto
0.152 s 16 KBi
#23
Correcto
0.16 s 16 KBi
#24
Correcto
0.149 s 16 KBi
#25
Correcto
0.15 s 16 KBi
#26
Correcto
0.4 s 21 KBi
#27
Tiempo límite excedido
0.317 s 29 KBi
#28
Correcto
0.428 s 24 KBi
#29
Correcto
0.422 s 24 KBi
#30
Correcto
0.451 s 24 KBi
#31
Correcto
0.417 s 21 KBi
#32
Correcto
0.404 s 20 KBi
#33
Correcto
0.382 s 21 KBi
#34
Correcto
0.381 s 21 KBi
Puntos totales: 98 / 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;
    }
}