반응형
1. 문제 번호 10807번
2. 문제 풀이
- 계속해서 에러가 뜸..
- > return에서 문제가 됐다고 추정
- 순차탐색은 반복문이 더 효율적 (재귀호출 보다)
- 이진탐색 = 정렬이 되어 있을때 + 중복 여부도 함께 고려할 것
- 순차탐색 = 정렬이 안되어 있을때 + 중복 여부도 함께 고려할 것
나의 문제풀이 방식 및 순서
-
3. 소스 인증
/*************
사용하지마세요.
*************/
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int size = Integer.parseInt(br.readLine());
int [] numList = new int[size];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i<size; i++){
numList[i] = Integer.parseInt(st.nextToken());
}
int target = Integer.parseInt(br.readLine());
//순차탐색
int numListFindIndex = findValue(numList, 0, size, target);
bw.write(String.valueOf(numListFindIndex));
bw.flush();
bw.close();
}
// 순차탐색 정렬이 안되어있을때 사용 + 중복 불허
public static int findValue(int [] numList, int begin, int end, int target){
for(int i = begin; i<end; i++){
if(numList[i]==target){
return i;
}
}
return 0;
}
// 이진탐색 정렬이 되어있을때 + 중복 허가
// public static int findValue(int [] numList,int begin,int end,int target){
// if(begin>end){
// return -1;
// } else {
// int middle = (begin + end)/2;
// if (numList[middle] ==target)
// return middle;
// int index = findValue(numList, begin, middle, target);
// if (index != -1)
// return index;
// else
// return findValue(numList, middle+1, end, target);
// }
// }
}
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int size = Integer.parseInt(br.readLine());
int[] numList = new int[size];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < numList.length; i++) {
numList[i] = Integer.parseInt(st.nextToken());
}
int target = Integer.parseInt(br.readLine());
//순차탐색_재귀호출
int count = countValue(numList, 0, size, target);
bw.write(String.valueOf(count) + "\n");
bw.flush();
bw.close();
br.close();
}
//못찾을때 0으로 해버리면 index와 헷갈림
public static int countValue(int [] numList, int begin, int end, int target){
int count = 0;
for(int i = begin; i < end; i++){
if(numList[i] == target){
count++; // 목표값을 찾으면 카운터 증가
}
}
return count; // 찾은 목표값의 개수 반환
}
}
4. 추가 개념
728x90
반응형
'알고리즘(BOJ) 문제풀이' 카테고리의 다른 글
[BOJ/백준] 1차원 배열_10818번_미완성 (0) | 2024.05.09 |
---|---|
[BOJ/백준] 1차원 배열_10871번 (0) | 2024.05.09 |
[BOJ/백준] 반복문_10951번 (0) | 2024.05.09 |
[BOJ/백준] 반복문_10952번 (0) | 2024.05.08 |
[BOJ/백준] 반복문_2439번 (0) | 2024.05.08 |