본문 바로가기
java 개발일지

코드업 기초100제, #1043 ~ #1052

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

🌼1043

📝 나머지 출력

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();
		System.out.println(a % b);

	}

}

 

 

🌼1044

📝 int 범위

-2,147,483,648 ~ 2,147,483,647

따라서 int보다 리터럴 범위가 큰 long으로 받아서 출력해줘야 한다.

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		n++;
		System.out.println(n);

	}

}

 

 

🌼1045

📝 format("%.2f",  (float) 변수)

묵시적 형변환 사용.

둘 중에 하나가 float(실수) 형이면 연산 결과도 float형으로 암묵적 형 변환을 하게 된다.

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();
//		float c = a / b;
		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);
		System.out.format("%.2f", ((float)a) / b);
//		System.out.println((float)(a / b));

	}

}

 

 

🌼1046

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();
		int c = sc.nextInt();
		System.out.println(a + b + c);
		System.out.format("%.1f", (((float)a) + b + c) / 3);
	}

}

 

 

🌼1047

📝 비트연산사 << , >>

10진수로 받은 숫자를 비트연산자를 이용하면 굳이 2진수로 받지 않아도 알아서 비트연산을 해버린다.

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		System.out.println(a << 1);
		

	}

}

 

 

🌼1048

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();
		System.out.println(a << b);

	}

}

 

 

🌼1049

📝 비교연산자

==, !=, <, >, <=, >=

비교 연산잔의 결과는 boolean이다.

import java.util.Scanner;

public class 코드업_1049 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
//		System.out.println(a > b);
		int c = 0;
		if (a > b) {
			c = 1;
		}
		else {
			c = 0;
		}
		System.out.println(c);
		

	}

}

 

📝 Compare

Boolean.compare(c, false) 는 c 가 false 와 같으면 0

c 와 false 가 다르면 1

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();
		
		boolean c = a > b;
		
		System.out.println(Boolean.compare(c, false));
		

	}

}

 

📝 삼항연산자

if 문 길게쓰기 싫을 때 사용. 조건 하나로 값이 나눠질때 사용!

조건 ?  참인경우 : 거짓일 경우

public class 코드업_1049 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		
		// 삼항 연산자 ( 조건 )? (참인 경우) : (거짓인 경우) 
		System.out.println(a > b? 1 : 0);

	}

}

 

 

🌼1050

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();
		System.out.println(a == b ? 1 : 0);

	}

}

 

 

🌼1051

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();
		System.out.println(a <= b? 1 : 0);

	}

}

 

 

🌼1052

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();
		System.out.println(a != b? 1 : 0);

	}

}

댓글