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

Envío 6217

Problema 0x32 - Sumar dos números muy grandes

  • Autor: rpedrazacoello
  • Fecha: 2022-05-26 01:30:42 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.162 s 25 KBi
#2
Correcto
0.076 s 16 KBi
#3
Correcto
0.093 s 17 KBi
#4
Correcto
0.099 s 16 KBi
#5
Correcto
0.089 s 16 KBi
#6
Correcto
0.098 s 16 KBi
#7
Correcto
0.148 s 14 KBi
#8
Correcto
0.095 s 16 KBi
#9
Correcto
0.096 s 16 KBi
#10
Correcto
0.099 s 16 KBi
#11
Correcto
0.1 s 16 KBi
#12
Correcto
0.088 s 16 KBi
#13
Correcto
0.078 s 16 KBi
#14
Correcto
0.094 s 16 KBi
#15
Correcto
0.153 s 14 KBi
#16
Correcto
0.157 s 14 KBi
#17
Correcto
0.089 s 16 KBi
#18
Correcto
0.107 s 17 KBi
#19
Correcto
0.096 s 16 KBi
#20
Correcto
0.075 s 16 KBi
#21
Correcto
0.09 s 16 KBi
#22
Correcto
0.086 s 16 KBi
#23
Correcto
0.093 s 17 KBi
#24
Correcto
0.139 s 15 KBi
#25
Correcto
0.082 s 16 KBi
#26
Correcto
0.09 s 15 KBi
#27
Correcto
0.098 s 16 KBi
#28
Correcto
0.111 s 17 KBi
#29
Correcto
0.178 s 19 KBi
#30
Correcto
0.195 s 24 KBi
#31
Correcto
0.32 s 20 KBi
#32
Correcto
0.213 s 27 KBi
#33
Correcto
0.194 s 20 KBi
#34
Correcto
0.327 s 21 KBi
#35
Correcto
0.204 s 22 KBi
#36
Correcto
0.34 s 18 KBi
#37
Correcto
0.168 s 23 KBi
#38
Correcto
0.315 s 18 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);
        String largeNumber1 = scanner.nextLine();
        String largeNumber2 = scanner.nextLine();
        char[] numberArray1 = largeNumber1.length() >= largeNumber2.length() ? largeNumber1.toCharArray() : largeNumber2.toCharArray();
        char[] numberArray2 = largeNumber1.length() >= largeNumber2.length() ? largeNumber2.toCharArray() : largeNumber1.toCharArray();

        int remain = 0;

        for(int i=numberArray1.length-1, j=numberArray2.length-1; i>=0; i--, j--){
            int number = convertCharToInt(numberArray1[i]);
            int numberToAdd = j >= 0 ? convertCharToInt(numberArray2[j]) : 0;

            int sum = number + numberToAdd + remain;
            if(sum >= 10){
                int newNumber = sum - 10;
                numberArray1[i] = convertIntToChar(newNumber);
                remain = 1;
            } else {
                numberArray1[i] = convertIntToChar(sum);
                remain = 0;
            }
        }

        if(remain == 1){
            System.out.print(1);
        }
        for(int i=0; i<numberArray1.length; i++){
            System.out.print(numberArray1[i]);
        }

    }

    private static int convertCharToInt(char c){
        switch (c){
            case '0': return 0;
            case '1': return 1;
            case '2': return 2;
            case '3': return 3;
            case '4': return 4;
            case '5': return 5;
            case '6': return 6;
            case '7': return 7;
            case '8': return 8;
            case '9': return 9;
            default:
                System.out.println("NOT GOOD"); return -1;
        }
    }

    private static char convertIntToChar(int x){
        switch (x){
            case 0: return '0';
            case 1: return '1';
            case 2: return '2';
            case 3: return '3';
            case 4: return '4';
            case 5: return '5';
            case 6: return '6';
            case 7: return '7';
            case 8: return '8';
            case 9: return '9';
            default:
                System.out.println("NOT GOOD"); return 'n';
        }
    }
}