[2단계. 사칙연산_JAVA]
▶ 10998. A*B
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
b = sc.nextInt();
System.out.println(a*b);
}
}
▶ 1008. A/B
- 입력 값이 정수라해서 int형으로 선언해줬더니 결과 값 또한 int형으로 밖에 못 출력해냈다. 따라서 문제에서 요구하는대로 길게 출력하기 위해선 a,b를 double형으로 선언해야한다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
double a,b;
a = sc.nextInt();
b = sc.nextInt();
if(a > 0 && b < 10) {
System.out.println(a/b);
}
else
return;
}
}
▶ 10869. 사칙연산
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
b = sc.nextInt();
if(a >= 1 && b <= 10000) {
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);
}
else
return;
}
}
▶ 10430. 나머지
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a,b,c;
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if(a >= 2 && c <= 10000) {
System.out.println((a+b)%c);
System.out.println((a%c+b%c)%c);
System.out.println((a*b)%c);
System.out.println((a%c*b%c)%c);
}
else
return;
}
}
▶ 2558. A+B-2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a;
a = sc.nextInt();
int b;
b = sc.nextInt();
if(a > 0 && b < 10) {
System.out.println(a+b);
}
else
return;
}
}
▶ 2839. 설탕배달
- a: 입력 받는 수, b: 몫
- 과정은 크게 두가지로 1. 입력 값이 5보다 큰 수일때, 2. 입력 값이 5보다 작은 수 일때 로 나눠서 풀었다. 1번 과정, 입력 값이 5보다 클때는 아래와 같이 다시 4가지로 나눈다.
(1). 5로 나눠떨어지는 수
(2). 5로 나눈 가장 큰 몫과 그 나머지가 3으로 나눠떨어지는 수
(3). 5로 나눈 가장 큰 몫보다 작은 몫과 그 나머지가 3으로 나눠떨어지는 수
(4). 3으로 나눠떨어지는 수
이렇게 여러 갈래로 나뉘기 때문에, 가장 중요한건 입력 값이 여기 저기 속하지 않고 한 갈래에만 속하도록 조건을 걸어줘야한다!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
if( 3 <= a && a <= 5000) {
if(a >= 5)
{
b = a/5;
if(a%5 == 0) {
System.out.println(b);
}
else if((a-(5*b))%3 == 0) {
System.out.println(b+((a-5*b)/3));
}
else if((a-(5*b))%3 != 0 && b > 1) {
int i = b;
while(i != 0) {
if((a-(5*i))%3 == 0)
{
System.out.println(i+(a-(5*i))/3);
break;
}
else
--i;
}
if(i == 0 && a%3 != 0) {
System.out.println("-1");
}
else if(i == 0 && a%3 ==0) {
b = a/3;
System.out.println(b);
}
}
else {
if(a%3 == 0)
{
System.out.println(a/3);
}
else
System.out.println("-1");
}
}
else
{
if(a == 3)
System.out.println("1");
else
System.out.println("-1");
}
}
else
return;
}
}
문제 출처: https://www.acmicpc.net/problem
'IT > BOJ' 카테고리의 다른 글
[백준알고리즘] if 문 1330번 정답 (0) | 2020.03.29 |
---|---|
[ 이클립스 런 단축키] (0) | 2018.07.19 |
[백준 알고리즘] 1단계. 입출력 (0) | 2018.07.07 |