๐ค ๋ฐฑ์ค 2588๋ฒ: ๊ณฑ์ (๋ธ๋ก ์ฆ3)
๋ฌธ์ ํ์ด
1์ฐจ ํ์ด
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
String b = br.readLine(); // ๋ฌธ์์ด ๋ถ๋ฆฌํ ์์ ์ด๊ธฐ์ ํ ๋ณํ X
String[] arr = b.split(""); // ํ ๊ธ์์ฉ ๋ถ๋ฆฌ
for (int i=2; i>=0; i--) {
int result = a * Integer.parseInt(arr[i]); // a * ๊ฐ๊ฐ์ b ์ซ์
System.out.println(result);
}
// a*b
System.out.println(a * Integer.parseInt(b));
}
}
๋ค๋ฅธ ํ์ด
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
String b = br.readLine();
int bLen = b.length();
for (int i = bLen - 1; i >= 0; i--) {
int digit = b.charAt(i) - '0';
System.out.println(a * digit);
}
System.out.println(a * Integer.parseInt(b));
}
}
โ๏ธ ์์ ํ ๋ถ๋ถ
- `split("")` ๋์ `charAt()`์ผ๋ก ์ง์ ์๋ฆฟ์ ์ ๊ทผ → ๋ถํ์ํ ๋ฐฐ์ด ์์ฑ ๋ฐฉ์ง
- `arr` ๋ฐฐ์ด ์ ๊ฑฐ → ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ๊ฐ์
- `b.charAt(i) - '0'`์ผ๋ก ๋ฌธ์ → ์ ์ ๋ณํ
- ์ซ์ํ ๋ฌธ์๋ฅผ `charAt()`์ผ๋ก ์ถ์ถํ๋ฉด charํ์ด๋ฏ๋ก intํ์ผ๋ก ๋ณํํ๊ธฐ ์ํด ์์คํค์ฝ๋๋ก ๋ณํํด์ผ ํจ
- '0' ๋๋ 48์ ๋นผ์ผ ์๋ํ๋๋ก ๊ณ์ฐ ๊ฐ๋ฅ
Stirng n = "123";
n.charAt(0); // 1
n.charAt(1); // 2
n.charAt(2); // 3
(int)n.charAt(0) + (int)n.charAt(1) // 49 + 50 = 99
'0' = 48
'1' = 49
'2' = 50
'3' = 51
์ฌ๊ธฐ์ -'0'(48)์ ํ๋ฉด ์ํ๋ ์ซ์๊ฐ ๋์ด
`n.charAt(0)-'0'` = 49-48 = 1
`n.charAt(1)-'0'` = 50-48 = 2
`n.charAt(2)-'0'` = 51-48 = 3
์ฐธ๊ณ ์๋ฃ
https://bright-landscape.tistory.com/270#google_vignette
์๋ฐ(JAVA) - ๋ฌธ์์ด(String) ๋ฉ์๋ - charAt(), replace(), substring(), indexOf(), contains(), split()
1. ๋ฌธ์์ถ์ถ - charAt() ๋งค๊ฐ๊ฐ์ผ๋ก ์ฃผ์ด์ง ์ธ๋ฑ์ค์ ๋ฌธ์๋ฅผ ๋ฆฌํดํ๋ค. ์ธ๋ฑ์ค๋ 0์์ "๋ฌธ์์ด์ ๊ธธ์ด-1" ๊น์ง์ ๋ฒํธ์ด๋ค. ex) 2. ๋ฌธ์์ด ๋์ฒด - replace() ๊ธฐ์กด ๋ฌธ์์ด์ ๊ทธ๋๋ก ๋๊ณ , ๋์ฒดํ ์๋ก์ด ๋ฌธ
bright-landscape.tistory.com
https://yeoncoding.tistory.com/418
[JAVA] charAt(i) -'0' ์ฌ์ฉ ์ด์
charAt์ด๋ string ํ์ ์ผ๋ก ๋ฐ์ ๋ฌธ์์ด์ char ํ์ ์ผ๋ก ํ ๊ธ์๋ง ๋ฐ๊ฒ ํด์ฃผ๋ ํจ์์ด๋ค. ์ซ์ํ ๋ฌธ์๋ฅผ CharAtํจ์๋ก ์ถ์ถํ๋ฉด charํ์ด๋ฏ๋ก int ํ์ผ๋ก ๋ณํํ๋ คํ๋ฉด ์์คํค์ฝ๋๋ก ๋ณํ๋๋ค. ๋ฐ๋ผ
yeoncoding.tistory.com
'Coding Test > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/Java] 2566๋ฒ: ์ต๋๊ฐ (๋ธ๋ก ์ฆ3) (0) | 2025.04.02 |
---|---|
[๋ฐฑ์ค/Java] 10818๋ฒ: ์ต์, ์ต๋ (๋ธ๋ก ์ฆ3) (0) | 2025.04.01 |
[๋ฐฑ์ค/Java] 10872๋ฒ: ํฉํ ๋ฆฌ์ผ (๋ธ๋ก ์ฆ3) (0) | 2025.03.31 |
[๋ฐฑ์ค/Java] 5597๋ฒ: ๊ณผ์ ์ ๋ด์ ๋ถ..? (๋ธ๋ก ์ฆ3) (0) | 2025.03.31 |
[๋ฐฑ์ค/Java] 10039๋ฒ: ํ๊ท ์ ์ (๋ธ๋ก ์ฆ4) (0) | 2025.03.30 |