본문 바로가기

자린고비 독학하기/자린고비 코딩하기

코딩 테스트 공부 시작 Java 04일 차 - Codeup 100제 (~1050)

반응형

 

■ 중기 목표: Java로 삼성 SW 테스트 A형 취득

 

기초 100제 C로 풀었던 입출력 문제를 다시 자바로 풀어 보았다.

codeup.kr/problemsetsol.php?psid=23

 

문제집 / 기초 100제

 

codeup.kr

 

주요했던 포인트 

  • 비트 연산자의 활용
  • 자바의 boolean 형 변환

 

1046: 정수 3개 입력받아 합과 평균 출력하기

array로 받는 메서드 하나 만들까 하다가 쉬운 문제 복잡하게 푸는 기분이라 관뒀다. 

import java.util.Scanner;

public class codeup1046 {
	public static void main(String[] args) {
		
		int a,b,c;
		Scanner scan = new Scanner(System.in);
		a = scan.nextInt();
		b = scan.nextInt();	
		c = scan.nextInt();	
		
		long sum = a+b+c;
		float ave = (float)sum/3;
		
		System.out.println(sum);
		System.out.printf("%.1f", ave);
		

}
	
	

}

 

 

1047: 정수 1개 입력받아 2배 곱해 출력하기

비트 연산자를 사용해서 2배 곱해 출력하는 문제이다.

*2를 해도 되지만, 비트 연산자를 활용하기 위해 다른 부분도 넣어 봤다.

import java.util.Scanner;

public class codeup1047 {
	public static void main(String[] args) {
		
		int a;
		Scanner scan = new Scanner(System.in);
		a = scan.nextInt();
		
        // if a = 50
        
		System.out.printf("%d", a<<1); // 100
		System.out.printf("%d", a>>1); // 25
        System.out.printf("%d", a<<2); // 200
        System.out.printf("%d", a>>2); // 25/2 = 12.5

}
}

 

1048: 한 번에 2의 거듭제곱 배로 출력하기

문제에서는 비트 연산자를 사용해서 푸는 것을 권장하는데, 나는 그 권장 부분을 안 보고 풀어서 Math.pow로 제곱을 해 버렸다. 비트 연산자 사용한 부분도 하단에 있음.

 

  • 비트 연산자 미사용
import java.util.Scanner;

public class codeup1048 {
	public static void main(String[] args) {
		
		double a,b;
		Scanner scan = new Scanner(System.in);
		a = scan.nextDouble();
		b = scan.nextDouble();	
		
		// Math.pow를 위해 double로 받아 주었다. 
		
		double pow = Math.pow(2, b);
        
        //2의 b승
        
		double result = a * pow;
		
        // a * 2의 b승
        
        
		System.out.println((int)result);
        
        // 소수점 삭제를 위한 int 캐스팅

}

}

 

  • 비트 연산자 사용
import java.util.Scanner;

public class codeup1048 {
	public static void main(String[] args) {
		
		int a,b;
		Scanner scan = new Scanner(System.in);
		a = scan.nextInt();
		b = scan.nextInt();	
		
		System.out.printf("%d", a<<b);
		
        // 비트 연산 시 앞에 있는 a 곱하기(<<) 혹은 나누기 (>>)
        // 뒤에 있는 인자에 2의 b승한 것
}

}

 

 

1049: 두 정수 입력받아 비교하기1

 

  • if 사용 버전

보통 아래 설명을 안 보고 풀기 때문에 (힌트 받는 느낌 안 좋아해서) if로 풀어서 정답은 냈지만, 아래 사족을 보니 이번에도 이용하라는 비교/관계연산자 내용이 있었다. 일단 if로 풀면 1과 2를 적어 주면 된다.

import java.util.Scanner;

public class codeup1048 {
	public static void main(String[] args) {
		
		int a,b;
		Scanner scan = new Scanner(System.in);
		a = scan.nextInt();
		b = scan.nextInt();	
		
		if(a>b) {
			System.out.println("1");
		}else {
			System.out.println("0");
		}
		
}

}

 

  • if 없는 버전 (비교/관계연산자 사용 버전)

C나 C++에서의 boolean은 바로 1/0으로 형변환이 가능하다. 그런데 자바는 그런 기능을 제공하지 않는다.

그래서 int compare 변수에 삼항 연산자로 ture의 boolean 값이 나오면 1이 나오도록 만들어 주었다.

import java.util.Scanner;

public class codeup1048 {
	public static void main(String[] args) {
		
		int a,b;
		Scanner scan = new Scanner(System.in);
		a = scan.nextInt();
		b = scan.nextInt();	
			
		int compare = (a>b)? 1:0;
		
		System.out.printf("%d", compare);
		
}
}

 

 

오늘도 완료~ 재미있다~

반응형