1) 큰 수 출력하기
설명
package com.company.array;
import java.util.ArrayList;
import java.util.Scanner;
public class PrintBigNumber {
public static void main(String [] ars) {
PrintBigNumber printBigNumber = new PrintBigNumber();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int [] numbers = new int[count];
for (int i = 0; i < count; i++) {
numbers[i] = in.nextInt();
}
System.out.println(printBigNumber.solution(numbers));
}
private ArrayList<Integer> solution(int [] numbers) {
ArrayList<Integer> result = new ArrayList<>();
result.add(numbers[0]);
for (int i=1; i<numbers.length; i++) {
if (numbers[i-1] < numbers[i]) {
result.add(numbers[i]);
}
}
return result;
}
}
Java
복사
2) 보이는 학생
설명
package com.company.array;
import java.util.Scanner;
public class SeeStudents {
public static void main(String [] ars) {
SeeStudents seeStudents = new SeeStudents();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int [] numbers = new int[count];
for (int i = 0; i < count; i++) {
numbers[i] = in.nextInt();
}
seeStudents.solution(numbers);
}
private void solution(int [] numbers) {
int count = 0;
int temp = 0;
for (int number : numbers) {
if (temp < number) {
temp = number;
count ++;
}
}
System.out.println(count);
}
}
Java
복사
3) 가위 바위 보
설명
package com.company.array;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String [] args) {
RockPaperScissors rockPaperScissors = new RockPaperScissors();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int [] numbersA = rockPaperScissors.initData(in, count);
int [] numbersB = rockPaperScissors.initData(in, count);
rockPaperScissors.solution(numbersA, numbersB, count);
}
int[] initData(Scanner in, int count) {
int [] numbers = new int[count];
for (int i=0; i<count; i++) {
numbers[i] = in.nextInt();
}
return numbers;
}
void solution(int [] numbersA, int [] numbersB, int count) {
char result;
for (int i = 0; i < count; i++) {
if (numbersA[i] == numbersB[i]) {
result = 'D';
} else if (numbersA[i] == 1 && numbersB[i] == 2) {
result = 'B';
} else if (numbersA[i] == 2 && numbersB[i] == 3) {
result = 'B';
} else if (numbersA[i] == 3 && numbersB[i] == 1) {
result = 'B';
} else {
result = 'A';
}
System.out.println(result);
}
}
}
Java
복사
4) 피보나지 수열
설명
package com.company.array;
import java.util.ArrayList;
import java.util.Scanner;
public class FibonacciNumbers {
public static void main(String [] ars) {
FibonacciNumbers fibonacciNumbers = new FibonacciNumbers();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
fibonacciNumbers.solution(count);
}
private void solution(int count) {
ArrayList<Integer> result = new ArrayList<>();
int [] arr = new int[count];
arr[0] = 1;
arr[1] = 1;
for (int i = 2; i < count; i++) {
arr[i] = arr[i-2] + arr[i-1];
}
for (int i : arr) {
System.out.println(i + " ");
}
}
}
Java
복사
5) 소수 갯수
설명
package com.company.array;
import java.util.Scanner;
public class DecimalCount {
public static void main(String [] ars) {
DecimalCount decimalCount = new DecimalCount();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
decimalCount.solution(count);
}
private void solution(int count) {
int decimalCount = 0;
int [] ch = new int[count+1];
for (int i=2; i<=count; i++) {
if(ch[i] == 0) {
decimalCount++;
for(int j=i; j<=count; j=j+i) {
ch[j] = 1;
}
}
}
System.out.println(decimalCount);
}
}
Java
복사
6) 뒤집은 소수
설명
public static void main(String [] ars) {
DecimalCount decimalCount = new DecimalCount();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int [] arr = new int[count];
for (int i=0; i<count; i++) {
arr[i] = in.nextInt();
}
decimalCount.solution(count, arr);
}
private void solution(int count, int[] arr) {
ArrayList<Integer> answer = new ArrayList<>();
for (int i : arr) {
int temp = i;
int res = 0;
while (temp>0) {
int t = temp%10;
res = res*10 + t;
temp = temp/10;
}
if (isPrime(res)) {
answer.add(res);
}
}
for (Integer integer : answer) {
System.out.println(integer + " ");
}
}
private boolean isPrime(int res) {
if (res == 1) return false;
for (int i=2; i<res; i++) {
if (res%i == 0) {
return false;
}
}
return true;
}
Java
복사
7) 격자판 최대합
설명
package com.company.array;
import java.util.Scanner;
public class SumGreatest {
public static void main(String [] ars) {
SumGreatest sumGreatest = new SumGreatest();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int [][] arr = new int[count][count];
for (int i=0; i< count; i++) {
for (int j=0; j< count; j++) {
arr[i][j] = in.nextInt();
}
}
sumGreatest.solution(count, arr);
}
private void solution(int count, int [][] arr) {
int answer = Integer.MIN_VALUE;
int sum1, sum2;
for (int i=0; i<count; i++) {
sum1 = 0;
sum2 = 0;
for (int j=0; j<count; j++) {
sum1 += arr[i][j];
sum2 += arr[j][i];
}
answer=Math.max(answer, sum1);
answer=Math.max(answer, sum2);
}
sum1 = 0;
sum2 = 0;
for (int i=0; i<count; i++) {
sum1 += arr[i][i];
}
for (int i=0; i<count; i++) {
sum2 += arr[i][count-i-1];
}
answer=Math.max(answer, sum1);
answer=Math.max(answer, sum2);
System.out.println(answer);
}
}
Java
복사
8) 봉우리
설명
package com.company.array;
import java.util.Scanner;
public class Peaks {
public static void main(String [] ars) {
Peaks peaks = new Peaks();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int [][] arr = new int[count][count];
for (int i=0; i<count; i++) {
for (int j=0; j<count; j++) {
arr[i][j] = in.nextInt();
}
}
peaks.solution(count, arr);
}
private void solution(int count, int [][] arr) {
int answer = 0;
int [] dx = {-1,0,1,0};
int [] dy = {0,1,0,-1};
for (int i=0; i<count; i++) {
for (int j=0; j<count; j++) {
boolean flag = true;
for (int k=0; k<4; k++) {
int nx = i+dx[k];
int ny = j+dy[k];
if (nx>=0 && nx<count && ny>=0 && ny<count && arr[i][j] <= arr[nx][ny]) {
flag = false;
break;
}
}
if (flag == true) answer++;
}
}
System.out.println(answer);
}
}
Java
복사