코드업 기초3. if ~ else
업데이트:
[1151] 10보다 작은 수
[입력] 5
[출력] small
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num < 10) System.out.print("small");
}
}
[1152] 10보다 작은 수 (else 버전)
[입력] 15
[출력] big
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (num >= 10) System.out.print("big");
else System.out.print("small");
}
}
[1153] 두 수의 대소 비교
[입력] 2 6
[출력] <
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
if(num1 > num2) System.out.print(">");
else if(num1 < num2) System.out.print("<");
else System.out.print("=");
}
}
[1154] 큰수 - 작은수
[입력] 5 7
[출력] 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
if(num1 > num2) System.out.print(num1-num2);
else System.out.print(num2-num1);
}
}
[1155] 7의 배수
[입력] 9
[출력] not multiple
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num % 7 == 0) System.out.print("multiple");
else System.out.print("not multiple");
}
}
[1156] 홀수 짝수 구별
[입력] 5
[출력] odd
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num % 2 == 0) System.out.print("even");
else System.out.print("odd");
}
}
[1157] 특별한 공 던지기 1
[입력] 550.213
[출력] win
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double num = sc.nextDouble();
if(num >= 50 && num <= 60) System.out.print("win");
else System.out.print("lose");
}
}
[1158] 특별한 공 던지기 2
[입력] 50
[출력] lose
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double num = sc.nextDouble();
if((num >= 30 && num <= 40) || (num >= 60 && num <= 70)) System.out.print("win");
else System.out.print("lose");
}
}
[1159] 특별한 공 던지기 3
[입력] 30
[출력] win
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double num = sc.nextDouble();
if((num >= 50 && num <= 70) || (num % 6 == 0)) System.out.print("win");
else System.out.print("lose");
}
}
[1160] 아르바이트 가는 날
[입력] 1
[출력] oh my god
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num == 1 || num == 3 || num == 5 || num == 7) System.out.print("oh my god");
else System.out.print("enjoy");
}
}
[1161] 홀수와 짝수 그리고 더하기
[입력] 2 3
[출력] 짝수+홀수=홀수
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int sum = num1 + num2;
if(num1 % 2 == 0) System.out.print("짝수+");
else System.out.print("홀수+");
if(num2 % 2 == 0) System.out.print("짝수=");
else System.out.print("홀수=");
if(sum % 2 == 0) System.out.print("짝수");
else System.out.print("홀수");
}
}
[1162] 당신의 사주를 봐 드립니다 1
[입력] 1902 2 10
[출력] 대박
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
int result = year - month + day;
if(result % 10 == 0) {
System.out.print("대박");
} else {
System.out.print("그럭저럭");
}
}
}
[1163] 당신의 사주를 봐 드립니다 2
[입력] 1502 2 10
[출력] 그럭저럭
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
int result = (year + month + day) / 100 % 10;
if(result % 2 == 0) {
System.out.print("대박");
} else {
System.out.print("그럭저럭");
}
}
}
[1164] 터널 통과하기 1
[입력] 170 168 175
[출력] CRASH
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int car = 170;
int tunnel1 = sc.nextInt();
int tunnel2 = sc.nextInt();
int tunnel3 = sc.nextInt();
if(car >= tunnel1 || car >= tunnel2 || car >= tunnel3) {
System.out.println("CRASH");
} else {
System.out.println("PASS");
}
}
}
[1165] 축구의 신 1
[입력] 74 2
[출력] 6
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ban1 = 0;
int ban2 = 2;
int gameTime = 90;
int inputTime = sc.nextInt();
int score = sc.nextInt();
for(int i = inputTime; i < gameTime; i+=5) {
ban1++;
}
System.out.println(ban1+score);
}
}
[1166] 윤년 판별
[입력] 2012
[출력] yes
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("yes");
else System.out.println("no");
}
}
[1167] 두 번째로 작은 수
[입력] 201 20 3
[출력] 20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
if(num1 > num2 && num1 > num3) {
if(num2 > num3) System.out.println(num2);
else System.out.println(num3);
} else if(num2 > num1 && num2 > num3){
if(num1 > num3) System.out.println(num1);
else System.out.println(num3);
} else {
if(num1 > num2) System.out.println(num1);
else System.out.println(num2);
}
}
}
[1168] 나이 계산 1
[입력] 790101 1
[출력] 34
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int birth = sc.nextInt();
int gender = sc.nextInt();
int year = 2012;
if(gender == 1 || gender == 2) {
System.out.println(year - (1900 + birth / 10000) + 1);
} else {
System.out.println(year - (2000 + birth / 10000) + 1);
}
}
}
[1169] 나이 계산 2
[입력] 790101 1
[출력] 34
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
int year = 2012 - age + 1;
if(year < 2000) {
System.out.println(year - 1900 + " 1");
} else {
System.out.println(year - 2000 + " 3");
}
}
}
[1170] 당신의 학번은? 1
[입력] 3 1 2
[출력] 3102
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int grade = sc.nextInt();
int ban = sc.nextInt();
int number = sc.nextInt();
String studentID = "";
if(number < 10) studentID = "" + grade + ban + "0" + number;
else studentID = "" + grade + ban + number;
System.out.println(studentID);
}
}
[1171] 당신의 학번은? 2
[입력] 2 7 15
[출력] 207015
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int grade = sc.nextInt();
int ban = sc.nextInt();
int number = sc.nextInt();
System.out.printf("%d%02d%03d", grade, ban, number);
}
}
[1180] 만능 휴지통
[입력] 90
[출력]
18
GOOD
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int num1 = num / 10;
int num2 = num % 10 * 10;
int result = (num2 + num1) * 2;
if(result >= 100) {
result -= 100;
}
System.out.println(result);
if(result <= 50) System.out.println("GOOD");
else System.out.println("OH MY GOD");
}
}
[1201] 정수 판별
[입력] 5
[출력] 양수
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num > 0) System.out.println("양수");
else if(num == 0) System.out.println("0");
else System.out.println("음수");
}
}
[1202] 등급 판정
[입력] 80
[출력] B
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
char grade = ' ';
if(score >= 90 && score <= 100) grade = 'A';
else if(score >= 80) grade = 'B';
else if(score >= 70) grade = 'C';
else if(score >= 60) grade = 'D';
else grade = 'F';
System.out.println(grade);
}
}
[1203] 비만도 측정 0
[입력] 15
[출력] 과체중
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int bmi = sc.nextInt();
if(bmi > 20) System.out.println("비만");
else if (bmi <= 20 && bmi > 10) System.out.println("과체중");
else System.out.println("정상");
}
}
[1204] 영어 서수로 표현하기
[입력] 2
[출력] 2nd
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num % 10 == 1 || num % 10 == 2 || num % 10 == 3) {
if(num == 11) System.out.println("11th");
else if(num == 12) System.out.println("12th");
else if(num == 13) System.out.println("13th");
else if(num % 10 == 1) System.out.println(num + "st");
else if(num % 10 == 2) System.out.println(num + "nd");
else if(num % 10 == 3) System.out.println(num + "rd");
} else System.out.println(num + "th");
}
}
[1205] 최댓값
[입력] 1 2
[출력] 3.000000
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double num1 = sc.nextDouble();
double num2 = sc.nextDouble();
double max = 0;
double add = num1 + num2;
double min = (num1 - num2 > num2 - num1 ? num1 - num2 : num2 - num1);
double mul = num1 * num2;
double div = (num1 / num2 > num2 / num1 ? num1 / num2 : num2 / num1);
double squt = (Math.pow(num1, num2) > Math.pow(num2, num1) ? Math.pow(num1, num2) : Math.pow(num2, num1));
if(max < add) max = add;
if(max < min) max = min;
if(max < mul) max = mul;
if(max < div) max = div;
if(max < squt) max = squt;
System.out.printf("%f", max);
}
}
if문과 삼항 연산자를 이용해 풀이했다. 제곱 부분은 어떻게 해야할지 모르겠어서 구글에 검색해봤더니 Math.pow(x, y)
라는 메소드를 찾을 수 있어서 해당 메소드를 사용해서 풀이했다.
[1206] 배수
[입력] 4 12
[출력] 4*3=12
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
if(num2 % num1 == 0) System.out.printf("%d*%d=%d", num1, num2 / num1, num2);
else if(num1 % num2 == 0) System.out.printf("%d*%d=%d", num2, num1 / num2, num1);
else System.out.println("none");
}
}
[1207] 윷놀이
[입력] 0 0 1 0
[출력] 도
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] inputs = new int[4];
int count = 0;
for(int i = 0; i < inputs.length; i++) {
inputs[i] = sc.nextInt();
if(inputs[i] == 1) {
count++;
}
}
switch(count) {
case 1 : System.out.println("도"); break;
case 2 : System.out.println("개"); break;
case 3 : System.out.println("걸"); break;
case 4 : System.out.println("윷"); break;
default : System.out.println("모");
}
}
}
[1210] 칼로리 계산하기
[입력] 1 2
[출력] angry
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int num1 = sc.nextInt();
int num2 = sc.nextInt();
switch(num1) {
case 1 : sum += 400; break;
case 2 : sum += 340; break;
case 3 : sum += 170; break;
case 4 : sum += 100; break;
default : sum += 70;
}
switch(num2) {
case 1 : sum += 400; break;
case 2 : sum += 340; break;
case 3 : sum += 170; break;
case 4 : sum += 100; break;
default : sum += 70;
}
if(sum > 500) System.out.println("angry");
else System.out.println("no angry");
}
}
[1212] 삼각형의 성립 조건
[입력] 2 3 4
[출력] yes
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();
int max = (a > b ? a : b) > c ? (a > b ? a : b) : c;
if(max == a) a = 0;
else if(max == b) b = 0;
else if(max == c) c = 0;
if(max < (a + b + c)) System.out.println("yes");
else System.out.println("no");
}
}
제일 긴 길이의 변수를 구하고, 그에 따라 더하는 변수들이 조건에 따라 다르므로, 제일 긴 길이의 변수의 값을 max 변수에 저장하고, max 변수와 같은 변수는 0을 대입해서 삼각형을 구하는 공식 조건 부분에서 세 개의 변수를 더했다.
[1214] 이 달은 며칠까지 있을까?
[입력] 2009 10
[출력] 31
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
int yundal = 0;
if((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month == 2 ) {
System.out.println("29");
} else {
switch(month) {
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 : System.out.println("31"); break;
case 2 : System.out.println("28"); break;
default : System.out.println("30");
}
}
}
}
[1216] 컨설팅 회사
[입력] 10 100 70
[출력] advertise
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();
if(b - c > a) System.out.println("advertise");
else if(b - c < a) System.out.println("do not advertise");
else System.out.println("does not matter");
}
}
[1218] 삼각형 판단하기
[입력] 3 3 3
[출력] 정삼각형
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();
if (c < a + b) {
if(a == b && b == c) System.out.println("정삼각형");
else if(a * a + b * b == c * c) System.out.println("직각삼각형");
else if(a == b || a == c || b == c) System.out.println("이등변삼각형");
else System.out.println("삼각형");
} else System.out.println("삼각형아님");
}
}
[1222] 축구의 신 2
[입력] 80 5 7
[출력] same
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int time = sc.nextInt();
int score1 = sc.nextInt();
int score2 = sc.nextInt();
int addScore = 0;
for(int i = time; i < 90; i+=5) {
++addScore;
}
if(score1 + addScore > score2) System.out.println("win");
else if (score1 + addScore == score2) System.out.println("same");
else System.out.println("lose");
}
}
[1224] 분수 크기 비교
[입력] 1 2 3 4
[출력] <
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
double d = sc.nextDouble();
if (a / b > c / d) System.out.println(">");
else if(a / b == c / d) System.out.println("=");
else if(a / b < c / d) System.out.println("<");
}
}
[1226] 이번 주 로또
[입력]
13 23 24 35 40 45 7
2 6 7 23 40 44
[출력] 0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] lottoNumber = new int[7];
int[] juhuiLotto = new int[6];
int count = 0;
boolean bonus = false;
for(int i = 0; i < lottoNumber.length; i++) {
lottoNumber[i] = sc.nextInt();
}
for(int i = 0; i < juhuiLotto.length; i++) {
juhuiLotto[i] = sc.nextInt();
}
for (int i = 0; i < lottoNumber.length; i++) {
for (int j = 0; j < juhuiLotto.length; j++) {
if (i < lottoNumber.length - 1) {
if (lottoNumber[i] == juhuiLotto[j]) {
count++;
}
} else {
if (lottoNumber[i] == juhuiLotto[j]) {
bonus = true;
}
}
}
}
switch(count) {
case 3 : System.out.println("5"); break;
case 4 : System.out.println("4"); break;
case 5 :
if(bonus == false) System.out.println("3");
else System.out.println("2");
break;
case 6 : System.out.println("1"); break;
default : System.out.println("0");
}
}
}
[1228] 비만도 측정 1
[입력] 170.0 80.0
[출력] 비만
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double height = sc.nextDouble();
double weight = sc.nextDouble();
double standardWeight = (height - 100) * 0.9;
double obesity = (weight - standardWeight) * 100 / standardWeight;
if(obesity <= 10) System.out.println("정상");
else if(obesity <= 20) System.out.println("과체중");
else System.out.println("비만");
}
}
[1229] 비만도 측정 2
[입력] 150 60
[출력] 과체중
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double height = sc.nextDouble();
double weight = sc.nextDouble();
double standardWeight = 0.0;
if(height < 150) {
standardWeight = height - 100;
} else if(height < 160) {
standardWeight = (height - 150) / 2 + 50;
} else {
standardWeight = (height - 100) * 0.9;
}
double obesity = (weight - standardWeight) * 100 / standardWeight;
if(obesity <= 10) System.out.println("정상");
else if(obesity <= 20) System.out.println("과체중");
else System.out.println("비만");
}
}
[1230] 터널 통과하기 2
[입력] 171 168 165
[출력] CRASH 168
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int car = 170;
int tunnel1 = sc.nextInt();
int tunnel2 = sc.nextInt();
int tunnel3 = sc.nextInt();
if(car >= tunnel1) System.out.println("CRASH " + tunnel1);
else if(car >= tunnel2) System.out.println("CRASH " + tunnel2);
else if(car >= tunnel3) System.out.println("CRASH " + tunnel3);
else System.out.println("PASS");
}
}
[1231] 계산기 1
[입력] 10+10
[출력] 20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String intStr1 = "";
String intStr2 = "";
int idx = 0;
char op = ' ';
char[] arr = new char[input.length()];
for (int i = 0; i < arr.length; i++) {
arr[i] = input.charAt(i);
if (arr[i] == '+' || arr[i] == '/' || arr[i] == '-' || arr[i] == '*') {
op = arr[i];
idx = i;
}
}
for (int i = 0; i < arr.length; i++) {
if ((48 <= arr[i] && arr[i] <= 57) && i < idx) {
intStr1 += arr[i];
} else if ((48 <= arr[i] && arr[i] <= 57) && i > idx) {
intStr2 += arr[i];
}
}
int num1 = Integer.valueOf(intStr1);
int num2 = Integer.valueOf(intStr2);
if((num1 == 0 || num2 == 0) && op == '/') {
return;
}
switch(op) {
case '+' : System.out.println(num1 + num2); break;
case '-' : System.out.println(num1 - num2);break;
case '/' : System.out.printf("%.2f", (double)num1 / num2);break;
case '*' : System.out.println(num1 * num2);break;
}
}
}
입력문을 정수와 연산자로 나누는 부분에서 애를 많이 먹었다. 맨 처음에는 char 배열로 정수 부분과 연산자 부분을 나누고 정수 부분을 따로 char 배열을 int 타입으로 변환해서 구하려했는데 아무래도 정수 입력 범위가 따로 정해져 있지 않아 경우의 수가 너무 많아서 다른 방법을 생각했다. 그래서 split을 이용해 정수 부분과 연산자 부분을 나누려했지만 계속 에러가 나는 바람에 결국 구글에 검색했다. String 타입에서 int 타입만 추출하는 방법이 다행히도 여러 방법으로 나왔다. 그 중에 그나마 익숙한 for문을 이용한 방법을 택했다.
댓글남기기