1. 큰 수 출력하기
public class Main01 {
public static ArrayList <Integer> solution(int n, int[]arr){
ArrayList<Integer> answer = new ArrayList<>();
answer.add(arr[0]);
for (int i=1; i<n; i++){
if (arr[i] > arr[i-1]){
answer.add(arr[i]);
}
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[n];
for (int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
for (int x : solution(n, arr)){
System.out.println(x + " ");
}
}
}
2. 보이는 학생
public class Main02 {
public static int solution(int n, int []arr){
int answer = 1;
int max = arr[0];
for (int i=1; i<n; i++){
if (arr[i]> max){
answer ++;
max = arr[i];
}
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[n];
for (int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
System.out.println(solution(n, arr));
}
}
3. 가위 바위 보
public class Main03 {
public static String solution(int n, int []arr1, int[]arr2){
String answer = "";
for (int i=0; i<n; i++){
if (arr1[i]==arr2[i]){
answer += "D";
}else if(arr1[i] ==1 && arr2[i]==3){
answer += "A";
}else if(arr1[i] ==2 && arr2[i]==1){
answer += "A";
}else if(arr1[i] ==3 && arr2[i]==2) {
answer += "A";
}else answer += "B";
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr1[] = new int[n];
int arr2[] = new int[n];
for (int i=0; i<n; i++){
arr1[i] = sc.nextInt();
}
for (int i=0; i<n; i++){
arr2[i] = sc.nextInt();
}
for (char x : solution(n , arr1, arr2).toCharArray())
System.out.println(x);
}
}
4. 피보나치 수열
public class Main04 {
public static int[] solution(int n){
int[] answer = new int[n];
answer[0]=1;
answer[1]=1;
for (int i=2; i<n; i++){
answer[i]= answer[i-2] + answer[i-1];
}return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int x : solution(n)){
System.out.println(x + " ");
}
}
}
5. 소수 (에라토스테네스 체)
public class Main05 {
public static int solution(int n){
int answer =0;
int[] ch = new int[n+1];
for (int i=2; i<=n ; i++){
if (ch[i]==0){
answer++;
for (int j=i; j<=n; j=j+i){
ch[j]=1;
}
}
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(solution(n));
}
}
- 소수가 되는 수의 배수를 지우면 남은건 소수가 된다
- 2부터 자기자신을 제외한 배수가 되는것을 지우기
6. 뒤집은 소수
public class Main06 {
public static boolean isPrime(int num){
if (num == 1)
return false;
for (int i=2; i<num; i++){
if (num %i == 0)
return false;
}
return true;
}
public static ArrayList<Integer> solution(int n, int[]arr){
ArrayList<Integer> answer = new ArrayList<>();
for(int i=0; i<n; i++){
int tmp = arr[i];
int res = 0;
while(tmp >0){
int t = tmp%10;
res = res*10 +t;
tmp = tmp/10;
}
if (isPrime(res))
answer.add(res);
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[n];
for (int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
for (int x : solution(n, arr)){
System.out.print(x + " ");
}
}
}
7. 점수계산
public class Main07 {
public static int solution(int n, int[]arr){
int cnt=0;
int answer =0;
for (int i=0; i<n; i++){
if (arr[i] == 1){
cnt++;
answer += cnt;
}else
cnt=0;
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for (int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
System.out.println(solution(n, arr));
}
}
8. 등수 구하기
public class Main08 {
public static int[] solution(int n, int []arr){
int[] answer = new int[n];
int max = answer[0];
for (int i=0; i<n; i++){
int cnt =1;
for (int j=0; j<n; j++){
if (arr[j]> arr[i])
cnt++;
}
answer[i]=cnt;
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] =new int[n];
for (int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
for (int x : solution(n, arr)){
System.out.print(x + " ");
}
}
}public class Main09 {
public static int solution(int n, int[][]arr){
int answer = Integer.MIN_VALUE;
int sum1;
int sum2;
for(int i=0; i<n; i++){
sum1=sum2=0;
for (int j=0; j<n; j++){
sum1 += arr[i][j];
sum2 += arr[j][i];
}
answer = Math.max(answer, sum1);
answer = Math.max(answer, sum2);
}
sum1 = sum2 = 0;
for (int i=0; i<n; i++){
sum1 += arr[i][i];
sum2 += arr[i][n-i-1];
}
answer = Math.max(answer, sum1);
answer = Math.max(answer, sum2);
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [][] arr = new int[n][n];
for (int i =0; i<n; i++){
for (int j=0 ; j<n; j++){
arr[i][j] = sc.nextInt();
}
}
System.out.print(solution(n, arr));
}
}
9. 격자판 최대합
public class Main09 {
public static int solution(int n, int[][]arr){
int answer = Integer.MIN_VALUE;
int sum1;
int sum2;
for(int i=0; i<n; i++){
sum1=sum2=0;
for (int j=0; j<n; j++){
sum1 += arr[i][j];
sum2 += arr[j][i];
}
answer = Math.max(answer, sum1);
answer = Math.max(answer, sum2);
}
sum1 = sum2 = 0;
for (int i=0; i<n; i++){
sum1 += arr[i][i];
sum2 += arr[i][n-i-1];
}
answer = Math.max(answer, sum1);
answer = Math.max(answer, sum2);
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [][] arr = new int[n][n];
for (int i =0; i<n; i++){
for (int j=0 ; j<n; j++){
arr[i][j] = sc.nextInt();
}
}
System.out.print(solution(n, arr));
}
}
Integer.MIN_VALUE
: 정수의 최솟값 (2147483647)
Integer.MIX_VALUE
: 정수의 최댓값 (-2147483647)
Math.max(int a, int b)
: 두 인자 값 중 큰 값을 리턴
Math.min(int a, int b)
: 두 인자 값중 작은 값을 리턴
10. 봉우리
public class Main10 {
public static int solution(int n, int arr[][]){
int [] dx = {-1, 0,1,0};
int [] dy = {0, 1 , 0, -1};
int answer = 0;
for (int i=0; i<n; i++){
for (int j=0; j<n; 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<n && ny>=0 && ny<n && arr[nx][ny] >= arr[i][j] ){
flag = false;
break;
}
}
if (flag)
answer++;
}
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [][]arr = new int[n][n];
for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
arr[i][j] = sc.nextInt();
}
}
System.out.println(solution(n, arr));
}
}
11. 임시반장 정하기
public class Main11 {
public static int solution(int n, int [][]arr){
int answer = 0, max=Integer.MIN_VALUE;
for(int i=1; i<n; i++){
int cnt=0;
for (int j=1; j<=n; j++){
for (int k=1; k<=5; k++){
if (arr[i][k]==arr[j][k]){
cnt++;
break;
}
}
}
if (cnt > max){
max = cnt;
answer = i;
}
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [][]arr = new int[n+1][6];
for (int i=1; i<=n; i++){
for (int j=1; j<=5; j++){
arr[i][j] = sc.nextInt();
}
}
System.out.println(solution(n, arr));
}
}