본문 바로가기
java 개발일지

백준 Class 1 java #2753 ~ #9498

by 노랑사랑팽이 2022. 9. 26.

🌼 백준 2753

윤년 찾기

📝 AND &&, OR ||

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int year = sc.nextInt();
		
		if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
			System.out.println(1);
		else
			System.out.println(0);
		
	}
}

 

 

🌼 백준 2884

45분 알람

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int m = sc.nextInt();
		
		if(m - 45 < 0)
		{
			m = 60 + m - 45;
			if(h == 0)
				h = 23;
			else
				h--;
		}
		else
			m -= 45;
		
		System.out.println(h + " " + m);
		
	}
}

 

 

🌼 백준 2908

📝 삼항 연산자

조건 ? True : False

 

✏️  추후 log 개념 활용해 digit 값 추출해보기

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		sc.close();
		
		int revA = (a % 10) * 100 + (a / 10 % 10) * 10 + (a / 100);
		int revB = (b % 10) * 100 + (b / 10 % 10) * 10 + (b / 100);
		
//		System.out.println(revA + " " + revB);
//		int max = revA;
//		if(max < revB)
//			max = revB;
//		
//		System.out.println(max);
		System.out.println(revA > revB ? revA : revB);
		
	}
}

 

 

🌼 백준 2920

✏️  추후 for문이나 list활용해 다른 방식으로 풀어보기

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String[] str = sc.nextLine().split(" ");
		sc.close();
		int[] num = new int[str.length];
		
		for(int i = 0; i < str.length; i++)
		{
			num[i] = Integer.parseInt(str[i]);
		}
		
		int[] asc = new int[] {1, 2, 3, 4, 5, 6, 7, 8};
		int[] des = new int[] {8, 7, 6, 5, 4, 3, 2, 1};
		
		if(Arrays.equals(num, asc))
			System.out.println("ascending");
		else if(Arrays.equals(num, des))
			System.out.println("descending");
		else
			System.out.println("mixed");
		
		
	}
}

 

 

🌼 백준 3052

📝 Set

set 은 다른값만 add 함

Hash 는 출력 순서 컴퓨터 마음대로~

그래서 index 값이 문제 풀이에 중요한 역할을 한다면, 배열이나 list사용해야 함.

import java.util.HashSet;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		HashSet<Integer> set = new HashSet<>();	
		
		for(int z=0;z<10;z++) {
			int n = sc.nextInt();
			set.add(n%42);
		}

		System.out.println(set.size());

	}
}

 

📝 List

📝 list.contains( )

boolean 함수로 true 나 false 둘 중에 하나만 나옴.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class one_3052 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		ArrayList<Integer> list = new ArrayList<>();

		for(int z=0;z<10;z++) {
			int n = sc.nextInt();
			if(list.contains(n % 42) == false)
				list.add(n % 42);
		}
		
		System.out.println(set.size());

	}
}

 

 

🌼 백준 8958

연속 O 면 score 올라감.

그러나 x 면 score 는 0으로 초기화

//import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		for(int i = 0; i < n; i++)
		{
			int cnt = 0;
			int scoreSum = 0;
			String str = sc.next();
//			int[] score = new int[str.length()]; 
//			Arrays.fill(score, 0);
			for(int j = 0; j < str.length(); j++)
			{
				if(str.charAt(j) == 'O')
				{
					cnt++;
//					score[j] = cnt;
					scoreSum += cnt;
				}
				else if(str.charAt(j) == 'X')
				{
					cnt = 0;
//					score[j] = cnt;
				}
//				scoreSum +=score[j];
			}
			System.out.println(scoreSum);
		}
		
	}
}

 

 

🌼 백준 9498

시험 성적

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		if(n >= 90 && n <= 100)
			System.out.println('A');
		else if(n >= 80 && n <= 89)
			System.out.println('B');
		else if(n >= 70 && n <= 79)
			System.out.println('C');
		else if(n >= 60 && n <= 69)
			System.out.println('D');
		else 
			System.out.println('F');
		
	}
}

'java 개발일지' 카테고리의 다른 글

백준 Class2 #1920  (0) 2022.09.28
백준 Class2 #2108  (0) 2022.09.28
백준 Class 1 java #2438 ~ #2742  (1) 2022.09.26
백준 Class 1 java #10951 ~ #1546  (0) 2022.09.26
백준 Class 1 java #1000 ~ #10950  (0) 2022.09.25

댓글