백준 단계별로 풀어보기 4단계 [1차원 배열]
- 단계 : 4-1
- 문제 : #10807
- 문제 제목 : 개수 세기
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int cnt = 0;
int[] arr = new int[n];
for(int i =0; i< n; i++){
arr[i] = in.nextInt();
}
int match = in.nextInt();
in.close();
for ( int j = 0; j < n; j++){
if ( match == arr[j]){
cnt++;
}
}
System.out.println(cnt);
}
}
- 단계 : 4-2
- 문제 : #10871
- 문제 제목 : X보다 작은 수
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
int x = in.nextInt();
for ( int i=0; i < n; i++){
arr[i] = in.nextInt();
}
in.close();
for ( int i =0; i< n; i++){
if ( x > arr[i]){
System.out.print(arr[i]+" ");
}
}
}
}
- 단계 : 4-3
- 문제 : #10818
- 문제 제목 : 최소, 최대
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for ( int i =0; i < n; i++){
arr[i] = in.nextInt();
}
in.close();
Arrays.sort(arr);
System.out.println(arr[0] + " " + arr[n-1]);
}
}
- 단계 : 4-4
- 문제 : #2562
- 문제 제목 : 최댓값
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] arr = new int[9];
int max = 0;
int n =0;
for ( int i =0; i < arr.length; i++){
arr[i] = in.nextInt();
}
in.close();
for (int j = 0; j < arr.length; j++){
if ( max < arr[j]){
max = arr[j];
n = j+1;
}
}
System.out.println(max);
System.out.println(n);
}
}
- 단계 : 4-5
- 문제 : #10810
- 문제 제목 : 공 넣기
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
int m = in.nextInt();
for ( int i =0; i< m; i++){
int f = in.nextInt();
int e = in.nextInt();
int ball = in.nextInt();
for ( int j=f-1; j < e; j++){
arr[j] = ball;
}
}
in.close();
for (int k = 0; k < arr.length; k++){
System.out.print(arr[k] + " ");
}
}
}
- 단계 : 4-6
- 문제 : #10813
- 문제 제목 : 공 바꾸기
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
int m = in.nextInt();
for ( int i=0; i < n; i++){
arr[i] = i+1;
}
for ( int j=0; j < m; j++){
int x = in.nextInt();
int y = in.nextInt();
int tmp = arr[x-1];
arr[x-1] = arr[y-1];
arr[y-1] = tmp;
}
for ( int k=0; k<n; k++){
System.out.print(arr[k] + " ");
}
}
}
- 단계 : 4-7
- 문제 : #5597
- 문제 제목 : 과제 안 내신 분..?
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] arr = new int[31];
for (int i = 0; i < 28; i++) {
int n = Integer.parseInt(br.readLine());
arr[n] = 1;
}
for (int i = 1; i <= 30; i++) {
if (arr[i] != 1) System.out.println(i);
}
}
}
- 단계 : 4-8
- 문제 : #3052
- 문제 제목 : 나머지
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader( new InputStreamReader(System.in));
int[] arr = new int[10];
boolean tf;
int cnt = 0 ;
for ( int i =0; i < 10; i++){
int n = Integer.parseInt(bf.readLine());
arr[i] = n%42;
}
for ( int j = 0; j < arr.length; j++){
tf = false;
for ( int k = j+1; k < arr.length; k++){
if ( arr[j] == arr[k]){
tf = true;
break;
}
}
if ( tf == false){
cnt++;
}
}
System.out.println(cnt);
}
}
- 단계 : 4-9
- 문제 : #10811
- 문제 제목 : 바구니 뒤집기
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[] arr = new int[n];
for ( int i=0; i < arr.length; i++){
arr[i] = i+1;
}
for(int j=0; j<m; j++) {
st = new StringTokenizer(bf.readLine(), " ");
int x = Integer.parseInt(st.nextToken())-1;
int y = Integer.parseInt(st.nextToken())-1;
for(int k=x; k<=y; k++, y--) {
int temp = arr[k];
arr[k] = arr[y];
arr[y] = temp;
}
}
for(int z=0; z<arr.length; z++)
System.out.print(arr[z] + " ");
bf.close();
}
}
- 단계 : 4-10
- 문제 : #1546
- 문제 제목 : 평균
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader( new InputStreamReader(System.in));
int n = Integer.parseInt(bf.readLine());
double[] arr = new double[n];
double sum = 0;
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
for (int i = 0; i< arr.length; i++) {
arr[i] = Double.parseDouble(st.nextToken());
}
Arrays.sort(arr);
for ( int j = 0; j < arr.length; j++){
sum += ((arr[j]/arr[arr.length-1])*100);
}
System.out.println(sum/ arr.length);
}
}