문제
https://www.acmicpc.net/problem/8595
이슈
다음은 히든넘버의 조건이다.
- 연속된 숫자는 한 히든 넘버이다.
- 두 히든 넘버 사이에는 글자가 적어도 한 개 있다.
- 히든 넘버는 6자리를 넘지 않는다.
두번째 조건이 내 코드에 적용을 할 필요가 없었던 문제
++ 문자인지 확인 방법 : Character.isLetter()
Char c = 'c';
//True 출력됨
if( Character.isLetter(c) ) System.out.println("TRUE");
첫번째 코드 - 실패 (숫자가 마지막에 오는 경우 생각 x)
package week10.baek.july9;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class B8595 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String str = br.readLine();
int count = 0;
String num = "";
for (int i = 0; i < n; i++) {
//알파벳인 경우
if( 65 <= str.charAt(i) && str.charAt(i) <= 90 ||
97 <= str.charAt(i) && str.charAt(i) <= 122 ) {
//앞서 있던 숫자가 있으면 count에 더해주기
//히든 넘버가 6자리를 넘어가거나 없으면 넘어감
if(num.equals("") ) continue;
else {
if( num.length() <= 6 ) {
count += Integer.parseInt(num);
}
num = "";
}
}
//숫자라면
else{
num += str.charAt(i);
}
}
System.out.println(count);
}
}
수정 코드 - 성공!
package week10.baek.july9;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class B8595 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String str = br.readLine();
int count = 0;
String num = "";
for (int i = 0; i < n; i++) {
//알파벳인 경우
if( 65 <= str.charAt(i) && str.charAt(i) <= 90 ||
97 <= str.charAt(i) && str.charAt(i) <= 122 ) {
//앞서 있던 숫자가 있으면 count에 더해주기
//히든 넘버가 6자리를 넘어가거나 없으면 넘어감
if(num.equals("") ) continue;
else {
if( num.length() <= 6 ) {
count += Integer.parseInt(num);
}
num = "";
}
}
//숫자라면
else{
num += str.charAt(i);
}
}
System.out.println(count);
}
}
최종 코드 (효율성 개선) - Character.isLetter(), StringBuiler 추가
package week10.baek.july9;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class B8595 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String str = br.readLine();
long count = 0; // count를 long으로 변경하여 오버플로우 방지
StringBuilder num = new StringBuilder(); // String 대신 StringBuilder 사용
for (int i = 0; i < n; i++) {
char c = str.charAt(i);
// 알파벳인 경우
if (Character.isLetter(c)) {
if (num.length() > 0 && num.length() <= 6) {
count += Long.parseLong(num.toString()); // num을 long으로 파싱
}
num.setLength(0); // num 초기화
} else {
// 숫자라면
num.append(c);
}
}
// 마지막 숫자를 처리
if (num.length() > 0 && num.length() <= 6) {
count += Long.parseLong(num.toString());
}
System.out.println(count);
}
}
++++ 다른 코드
출처
https://hunucho.tistory.com/m/550
Baekjoon 8595 히든 넘버 JAVA
8595번: 히든 넘버 첫째 줄에 단어의 길이 n (1 ≤ n ≤ 5,000,000)이 주어진다. 둘째 줄에는 단어가 주어진다. 단어는 알파벳 대/소문자와 숫자(0-9)로 이루어져 있다. www.acmicpc.net 문제 단어에 숫자가
hunucho.tistory.com
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();
long ans=0;
StringTokenizer st = new StringTokenizer(br.readLine().replaceAll("[a-zA-Z]", " "));
while(st.hasMoreTokens())
ans+=Integer.parseInt(st.nextToken());
System.out.println(ans);
}
}
'‡ CODING TEST STUDY ‡ > º 백준' 카테고리의 다른 글
[백준 | Java Silver II ] (#1260) DFS와 BFS (0) | 2024.07.11 |
---|---|
[백준 | Java Bronze I ] (#2804) 크로스워드 만들기 (0) | 2024.07.11 |
[백준 | Java Bronze I ] (#14659) 한조서열정리하고옴ㅋㅋ (0) | 2024.07.10 |
[백준 | Java Bronze I ] (#11557) Yangjojang of The Year (0) | 2024.07.10 |
[백준 | Java Bronze III ] (#19532) 수학은 비대면강의입니다 (0) | 2024.07.08 |