[백준 1225번] 이상한 곱셈
[백준 1225번] 이상한 곱셈
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package _1월_3주차; import java.util.Scanner; public class 백준_손수경_정답_1225 {     public static void main(String[] args) {         //sol 2. 배열을 이용한 경우         Scanner sc = new Scanner(System.in);         String a = sc.next();          String b = sc.next();         String integerA[] = a.split("");         String integerB[] = b.split("");         long fristSum = 0;         long secondSum = 0;         //Integer.parseInt() -> 문자형을 10진수로 바꿔줌         for (int i = 0; i < integerA.length; i++) {             fristSum += Integer.parseInt(integerA[i]);         }         for (int i = 0; i < integerB.length; i++) {             secondSum += Integer.parseInt(integerB[i]);         }         System.out.println(fristSum * secondSum);     } } /* sol 1. 정수형을 이용한 경우 -> 런타임 에러 발생 Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int fristSum = 0; int secondSum = 0; int temp1, temp2; while (true) {     temp1 = a % 10;     a /= 10;     temp2 = b % 10;     b /= 10;     fristSum += temp1;     secondSum += temp2;     if (a == 0 && b == 0) {         break;     } } System.out.println(fristSum * secondSum); */ | cs | 
sol 1같은 경우는 정수형으로 푼 문제였는데 계속 런타임 에러가 떠서 찾아보니 문자형으로 해서 풀어야 된다고 하였다,,,(대체 왜,,,?) 문자형을 이용해서 풀려면 우선 그 배열들을 문자 하나씩 배열에 넣어준 다음에 그 요소들을 Integer.parseInt() 를 통해서 10진수로 바꾸어 주어서 계산해 주었다.
 
      
댓글남기기