1 분 소요

백준 실습

사칙연산

image-20240622174723498

image-20240622174834736

public class BackJoon_1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a, b;
		
		a = sc.nextInt();
		b = sc.nextInt();
		
		System.out.println("더하기 = " + (a+b));
		System.out.println("빼기 = " + (a - b));
		System.out.println("곱하기 = " + (a*b));
		System.out.println("나누기 = " + (a/b));
		System.out.println("나머지 = " + (a%b));
	}
}

시험 성적

image-20240622175103769

image-20240622180150191

해당 문제는 if - else로 작성

if(조건식1){
    조건문1 이 true일때 실행문
}else if(조건식2){
    조건문2가 true일때 실행문
}else
    조문식이 false일때 실행문

작성한 코드

Scanner sc = new Scanner(System.in);
		
		int a;
		a = sc.nextInt();
		
		if(a >= 90) {
			System.out.println("A");
		}else if(a >= 80 ) {
			System.out.println("B");
		}else if(a >= 70) {
			System.out.println("C");
		}else if(a >= 60) {
			System.out.println("D");
		}else
			System.out.println("F");

두 수 비교하기

image-20240622180227899

image-20240622180242190

해당 문제도 if 문을 사용

Scanner sc = new Scanner(System.in);
		
		int a,b;
		a = sc.nextInt();
		b = sc.nextInt();
		
		if(a > b) {
			System.out.println(">");
		}else if(a < b) {
			System.out.println("<");
		}else if(a == b) {
			System.out.println("==");
		}

구구단

image-20240622180636839

image-20240622180643422

해당 구문은 for 문으로 작성

for(초기화식; 조건식; 증감식){
    실행문
}

해당 문법으로 작성

Scanner sc = new Scanner(System.in);
		int a;
		
		a = sc.nextInt();
		
		for(int i = 1; i <= 9; i++) {
			System.out.println(a +" * " + i + " = " + (a * i));
		}

나머지

image-20240622181500647

image-20240622181519033

if 문을 사용하고 if문을 좀더 알아보기 쉽게

int plus_1, plus_2

int times_1, times_2

를 만들었다.

Scanner sc = new Scanner(System.in);
		
		int a,b,c;
		a = sc.nextInt();
		b = sc.nextInt();
		c = sc.nextInt();
		
		int plus_1 = ((a+b)%c);
		int plus_2 = ((a%c) + (b%c) % c);
		
		int times_1 = ((a*b)%c);
		int times_2 = (((a%c) * (b%c))%c);
		
		if( plus_1 == plus_2 ) {
			System.out.println("plus_1 = " + plus_1);
			System.out.println("plus_2 = " + plus_2);
			System.out.println(" 같다");
		}else{
			System.out.println("plus_1 = " + plus_1);
			System.out.println("plus_2 = " + plus_2);
			System.out.println("틀리다");
		}
		
		if( times_1 == times_2) {
			System.out.println("times_1 = " + times_1);
			System.out.println("times_2 = " + times_2);
			System.out.println(" 같다");
		}else{
			System.out.println("times_1 = " + times_1);
			System.out.println("times_2 = " + times_2);
			System.out.println("틀리다");
		}

해당 코드는 알아 보기 쉽게 적은 것이고

제출 코드

Scanner sc = new Scanner(System.in);
		int a,b,c;
		a = sc.nextInt();
		b = sc.nextInt();
		c = sc.nextInt();
		
		int plus_1 = ((a+b)%c);
		int plus_2 = (((a%c) + (b%c)) % c);
		
		int times_1 = ((a*b)%c);
		int times_2 = (((a%c) * (b%c))%c);
		
		if( plus_1 == plus_2 ) {
			System.out.println(plus_1);
			System.out.println(plus_2);
		}		
		if( times_1 == times_2) {
			System.out.println(times_1);
			System.out.println(times_2);
		}

댓글남기기