Tags
- Java
- ๊ทธ๋ํ ํ์
- ๊ทธ๋ํ ์ด๋ก
- CodingTest
- greedy
- stack
- SpringBoot
- Brute Force Algorithm
- dfs
- ๊น์ด ์ฐ์ ํ์
- BOJ
- ์๋ฃ๊ตฌ์กฐ
- Study
- BFS
- ์ํ
- ์ ์๋ก
- ์๋ฎฌ๋ ์ด์
- LV2
- sort
- ๊ตฌํ
- queue
- ๋ฌธ์์ด
- ์ ๋ ฌ
- DP
- ๊ต์ฌ
- ๋ฐฑํธ๋ํน
- Python
- ๋๋น ์ฐ์ ํ์
- Dynamic Programming
- PGM
Archives
๊ธฐ๋ก๋ฐฉ
BOJ_1065 : ํ์ ๋ณธ๋ฌธ
๐ธ ๋ฌธ์ ๋ถ์ ๐ธ
- 1๋ถํฐ N์ค์ ๊ฐ ์๋ฆฌ์๊ฐ ๋ฑ์ฐจ์์ด๋ก ์ด๋ฃจ์ด์ง 'ํ์'์ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ค.
๐ธ ์ฝ๋ ๐ธ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int answer = 0;
for (int i = 1; i <= n; i++){
String s[] = Integer.toString(i).split("");
boolean flag = true;
if (s.length > 1){
int diff = Integer.parseInt(s[1]) - Integer.parseInt(s[0]);
for (int j = 1; j < s.length-1; j++){
if (Integer.parseInt(s[j+1]) - Integer.parseInt(s[j]) != diff) {
flag = false;
break;
}
}
}
if (flag)
answer++;
}
System.out.println(answer);
}
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- i๋ฅผ 1๋ถํฐ n๊น์ง ์ฆ๊ฐ์ํค๋ฉฐ ํ์์ธ์ง ํ๋จํ๋ค.
- i๋ฅผ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋ณํํด ๊ฐ ์๋ฆฌ์์ ์ฐจ์ด๋ฅผ ๊ตฌํ๋ค.
- 2์๋ฆฌ ์ดํ๋ฉด ๋ฌด์กฐ๊ฑด ๋ฑ์ฐจ์์ด์ด๊ณ ๊ทธ ๋ค์๋ถํฐ๋ ์ผ์ ํ ์ฐจ์ด๋ฅผ ๊ฐ๋์ง ํ์ธํ๋ค.
- ํ์๋ก ํ์ ๋๋ฉด answer์ ์ฆ๊ฐ์ํจ๋ค.
- ํ์์ ๊ฐ์ answer๋ฅผ ์ถ๋ ฅํ๋ค.
๐ธ end ๐ธ
- ๋ค์๋ณด๋ 1์๋ฆฌ๊ฐ ์๋๋ผ 2์๋ฆฌ๊น์ง๋ ๋ชจ๋ ๋ฌด์กฐ๊ฑด ๋ฑ์ฐจ์์ด์ด์๋ค.
์ฝ๋์ for๋ฌธ ์ ์กฐ๊ฑด์ if(s.length > 2)๋ก ์์ ํด์ผํ๋ค.
728x90
'CodingTest > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ_2217 : ๋กํ (0) | 2022.10.11 |
---|---|
BOJ_2714 : ๋ฌธ์๋ฅผ ๋ฐ์ ์นํ์ด (0) | 2022.10.10 |
BOJ_2635 : ์ ์ด์ด๊ฐ๊ธฐ (0) | 2022.10.08 |
BOJ_2628 : ์ข ์ด์๋ฅด๊ธฐ (0) | 2022.10.06 |
BOJ_2622 : ์ผ๊ฐํ๋ง๋ค๊ธฐ (0) | 2022.10.05 |