Advertisement
Georgi_Benchev

Untitled

Oct 9th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. package Telerik_Alpha_Java_2024.CodingTasks_1;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Task3_BigNumbers {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int[] sizes = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  11.         int[] numbersOne = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  12.         int[] numbersTwo = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  13.  
  14.         int[] output = new int[Math.max(sizes[0], sizes[1])];
  15.  
  16.         for (int i = 0; i < output.length; i++) {
  17.             int numOne;
  18.             int numTwo;
  19.             if (numbersOne.length - 1 >= i) {
  20.                 numOne = numbersOne[i];
  21.             } else {
  22.                 numOne = 0;
  23.             }
  24.             if (numbersTwo.length - 1 >= i) {
  25.                 numTwo = numbersTwo[i];
  26.             } else {
  27.                 numTwo = 0;
  28.             }
  29.  
  30.             int remainingNum = 0;
  31.             int finalNum = output[i] + numOne + numTwo;
  32.             if (finalNum >= 10) {
  33.                 remainingNum = finalNum / 10;
  34.                 if (i == output.length - 1) {
  35.                     output[i] = finalNum;
  36.                 } else {
  37.                     output[i + 1] = remainingNum;
  38.                     output[i] = finalNum % 10;
  39.                 }
  40.  
  41.             } else {
  42.                 output[i] = finalNum;
  43.             }
  44.         }
  45.  
  46.         for (int num : output) {
  47.             System.out.print(num + " ");
  48.         }
  49.  
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement