forDevLife
[코드업] 기초 100제 JAVA 본문
- 코드업 100제 링크 : codeup.kr/problemset.php?search=%EA%B8%B0%EC%B4%88100%EC%A0%9C
1001 : 출력하기 01
public class HelloWorldMain {
public static void main(String[] args) {
System.out.println("문장");
}
}
1002 : 출력하기 02
public class HelloWorldMain {
public static void main(String[] args) {
System.out.println("hello world");
}
}
1003 : 출력하기 03
public class HelloWorldMain {
public static void main(String[] args) {
System.out.println("hello" + "\n" + "world");
}
}
1004 : 출력하기 04
public class HelloWorldMain {
public static void main(String[] args) {
System.out.println("'hello'");
}
}
1005 : 출력하기 05
public class HelloWorldMain {
public static void main(String[] args) {
System.out.println(" \"hello\" ");
}
}
1006 : 출력하기 06
public class HelloWorldMain {
public static void main(String[] args) {
System.out.println("!@#$%^&*()");
}
}
1007 : 출력하기 07
public class HelloWorldMain {
public static void main(String[] args) {
System.out.println("C:\\Download\\hello.cpp");
}
}
- 백슬래시 문자 \를 출력하기 위해서는 \\로 작성해야 함
1008 : 출력하기 08
public class HelloWorldMain {
public static void main(String[] args) {
System.out.println("\u250C\u252C\u2510\n");
System.out.println("\u252C\u250C\u2510\n");
}
}
- \u 를 이용하여 유니코드를 출력할 수 있다.
1010 : 정수 1개 입력받아 그대로 출력하기
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
System.out.println(N);
}
}
1011 : 문자 1개 입력받아 그대로 출력하기
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char C = sc.next().charAt(0);
System.out.println(C);
}
}
- charAt을 이용, 특정 인텍스 리턴
1012 : 실수 1개 입력받아 그대로 출력하기
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float x = sc.nextFloat();
System.out.println(x);
}
}
1013 : 정수 2개 입력받아 그대로 출력하기
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x, y;
x = sc.nextInt();
y = sc.nextInt();
System.out.printf("%d %d\n", x, y);
}
}
1014 : 문자 2개 입력받아 순서 바꿔 출력하기
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char x, y;
x = sc.next().charAt(0);
y = sc.next().charAt(0);
System.out.printf("%c %c\n", y, x);
}
}
1015 : 실수 입력받아 둘째 자리까지 출력하기
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float x = sc.nextFloat();
System.out.printf("%.2f\n", x);
}
}
1017 : 정수 1개 입력받아 3번 출력하기
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
System.out.println(x+" "+x+" "+x);
}
}
1018 : 시간 입력받아 그대로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HelloWorldMain {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
1019 : 연월일 입력받아 그대로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String a = br.readLine();
String[] arr = a.split("\\.");
int year = Integer.valueOf(arr[0]);
int month = Integer.valueOf(arr[1]);
int day = Integer.valueOf(arr[2]);
System.out.printf("%04d.%02d.%02d\n", year, month, day);
}
}
- 점(.)은 하나의 문자와 대응하는 일종의 메타 문자이다. 문자 그대로 특수문자를 구분자로 사용하고 싶을 경우, 이스케이프 \\를 붙여준다.
- parsing 조건을 추가할 수 있다. 예를 들어 '-', '@', '.' 세 가지 구분자를 기준으로 진행하고 싶다면, split("-|@|\\.") 라고 작성한다. (| 사용)
- valueOf -> wrapper(Integer)로 반환 / parseInt -> int로 반환
1020 : 주민번호 입력받아 형태 바꿔 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String a = br.readLine();
String[] arr = a.split("-");
for(String b : arr) {
System.out.printf(b);
}
System.out.println();
}
}
1021 : 단어 1개 입력받아 그대로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String a = br.readLine();
System.out.println(a);
}
}
1022 : 문장 1개 입력받아 그대로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String a = br.readLine();
System.out.println(a);
}
}
1023 : 실수 1개 입력받아 부분별로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
String[] arr = s1.split("\\.");
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
1024 : 단어 1개 입력받아 부분별로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
String[] arr = s1.split("");
for(String s : arr) {
System.out.println("'"+s+"'");
}
}
}
1025 : 정수 1개 입력받아 나누어 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
String[] arr = s1.split("");
int num = arr.length - 1;
for(String s : arr) {
System.out.printf("[");
System.out.printf(s);
for(int i = 0; i < num; i++)
System.out.printf("0");
System.out.printf("]");
System.out.println();
num -= 1;
}
}
}
1026 : 시분초 입력받아 분만 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
String[] arr = s1.split(":");
System.out.println(arr[1]);
}
}
1027 : 년월일 입력받아 형식 바꿔 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
String[] arr = s1.split("\\.");
System.out.printf("%2s-%2s-%4s", arr[2], arr[1], arr[0]);
}
}
1028 : 정수 1개 입력받아 그대로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
long d1 = sc.nextLong();
System.out.println(d1);
}
}
1029 : 실수 1개 입력받아 그대로 출력하기2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
double d1 = sc.nextDouble();
System.out.println(d1);
}
}
1030 : 정수 1개 입력받아 그대로 출력하기3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
long d1 = sc.nextLong();
System.out.println(d1);
}
}
1031 : 10진 정수 1개 입력받아 8진수로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String a = Integer.toOctalString(sc.nextInt());
System.out.println(a);
}
}
- Integer.toOctalString(int)를 이용하여 10진수를 8진수로 변환할 수 있다.
1032 : 10진 정수 1개 입력받아 16진수로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String a = Integer.toHexString(sc.nextInt());
System.out.println(a);
}
}
1033 : 10진 정수 1개 입력받아 16진수로 출력하기2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String a = Integer.toHexString(sc.nextInt());
System.out.println(a.toUpperCase());
}
}
- String의 upper / lowercase를 이용
1034 : 8진 정수 1개 입력받아 10진수로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = Integer.valueOf(sc.next(), 8);
System.out.println(a);
System.out.printf("%o\n", a);
}
}
- sc로 읽어온 값을 8진수로 변환하여 a에 넣는다.
- 그냥 출력하면 10진수로 출력, %o를 이용해서 출력하면 8진수 그대로 출력된다.
1035 : 16진 정수 1개 입력받아 8진수로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = Integer.valueOf(sc.next(), 16);
System.out.printf("%o\n", a);
}
}
1036 : 영문자 1개 입력받아 10진수로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = (int)(sc.next().charAt(0));
System.out.println(a);
}
}
- 영문자 하나이므로, char -> int로 강제 형변환을 해주는 방법을 사용한다.
- Integer.parseInt()에 'A'가 입력되면 NumberFormatException이 발생한다.
여기에는 숫자가 입력되어야 하며, 문자 String(예를 들어 "65") -> 숫자(65)로 해주는 역할이다. 'A'는 숫자가 아니므로 에러가 발생한다.
1037 : 정수 입력받아 아스키 문자로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.printf("%c\n", a);
System.out.println((char)a);
}
}
- 정수 > 아스키 : (char)sc.nextInt();
- 아스키 > 정수 : (int)sc.next().charAt(0);
1038 : 정수 2개 입력받아 합 출력하기1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextInt() + sc.nextInt());
}
}
1039 : 정수 2개 입력받아 합 출력하기2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextLong() + sc.nextLong());
}
}
1040 : 정수 1개 입력받아 부호 바꿔 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println(-a);
}
}
1041 : 문자 1개 입력받아 다음 문자 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
char a = (char)(sc.next().charAt(0)+1);
System.out.println(a);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
char a = sc.next().charAt(0);
System.out.println(++a);
}
}
- 또는 두 번째 같이 ++a 증감연산을 통해 출력한다. println(a+1)로 하면 아스키코드에 해당하는 숫자가 증가하여 출력된다.(A -> 66출력)
1042 : 정수 2개 입력받아 나눈 몫 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextInt() / sc.nextInt());
}
}
1043 : 정수 2개 입력받아 나눈 나머지 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextInt() % sc.nextInt());
}
}
1044 : 정수 1개 입력받아 1 더해 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextLong() + 1);
}
}
1045 : 정수 2개 입력받아 자동 계산하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int 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);
System.out.printf("%.2f\n", (float)a/b);
}
}
1046 : 정수 3개 입력받아 합과 평균 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
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.printf("%.1f\n", (float)((a+b+c)/3));
}
}
1047 : 정수 1개 입력받아 2배 곱해 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println(a << 1);
}
}
1048 : 한 번에 2의 거듭제곱 배로 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a << b);
}
}
1049 : 두 정수 입력받아 비교하기1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a > b ? 1 : 0);
}
}
1050 : 두 정수 입력받아 비교하기2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a == b ? 1 : 0);
}
}
1051 : 두 정수 입력받아 비교하기3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(b >= a ? 1 : 0);
}
}
1052 : 두 정수 입력받아 비교하기4
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a != b ? 1 : 0);
}
}
1053 : 참 거짓 바꾸기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println( a == 0 ? 1 : 0);
}
}
1054 : 둘 다 참일 경우만 참 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println( (a == 1) && (b == 1) ? 1 : 0);
}
}
1055 : 하나라도 참이면 참 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println( (a == 1) || (b == 1) ? 1 : 0);
}
}
1056 : 참/거짓이 서로 다를 때에만 참 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println( (a==1&&b==0)||(a==0&&b==1) ? 1 : 0);
}
}
1057 : 참/거짓이 서로 같을 때에만 참 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println( (a==1&&b==1)||(a==0&&b==0) ? 1 : 0);
}
}
1058 : 둘 다 거짓일 경우만 참 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
boolean c = sc.nextBoolean();
boolean d = sc.nextBoolean();
System.out.println( (a==0&&b==0) ? 1 : 0);
System.out.println(!(c||d));
}
}
- int말고 boolean으로도 가능하다.
1059 : 비트단위로 NOT하여 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println(~a);
}
}
1060 : 비트단위로 AND하여 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a & b);
}
}
1061 : 비트단위로 OR하여 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a | b);
}
}
1062 : 비트단위로 XOR하여 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a ^ b);
}
}
1063 : 두 정수 입력받아 큰 수 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a > b ? a : b);
}
}
1064 : 정수 3개 입력받아 가장 작은 수 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println(a > b ? (c > b ? b : c) : (a > c ? c : a));
}
}
1065 : 정수 3개 입력받아 짝수만 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if(a%2 == 0) System.out.println(a);
if(b%2 == 0) System.out.println(b);
if(c%2 == 0) System.out.println(c);
}
}
1066 : 정수 3개 입력받아 짝/홀 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if(a%2 == 0)
System.out.println("even");
else
System.out.println("odd");
if(b%2 == 0)
System.out.println("even");
else
System.out.println("odd");
if(c%2 == 0)
System.out.println("even");
else
System.out.println("odd");
}
}
1067 : 정수 1개 입력받아 분석하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a < 0)
System.out.println("minus");
else
System.out.println("plus");
if(a % 2 == 0)
System.out.println("even");
else
System.out.println("odd");
}
}
1068 : 정수 1개 입력받아 평가 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a >= 90)
System.out.println("A");
else if (a >= 70 && a < 90)
System.out.println("B");
else if (a >= 40 && a < 70)
System.out.println("C");
else
System.out.println("D");
}
}
1069 : 평가 입력받아 다르게 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
switch(sc.next())
{
case "A":
System.out.println("best!!");
break;
case "B":
System.out.println("good!!");
break;
case "C":
System.out.println("run!");
break;
case "D":
System.out.println("slowly~");
break;
default:
System.out.println("what?");
break;
}
}
}
1070 : 월 입력받아 계절 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
switch(sc.nextInt())
{
case 12:
case 1:
case 2:
System.out.println("winter");
break;
case 3:
case 4:
case 5:
System.out.println("spring");
break;
case 6:
case 7:
case 8:
System.out.println("summer");
break;
default:
System.out.println("fall");
break;
}
}
}
1071 : 0 입력될 때까지 무한 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while(true) {
int a = sc.nextInt();
if(a != 0)
System.out.println(a);
else
break;
}
}
}
1072 : 정수 입력받아 계속 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] arr = new int[num];
for(int i=0; i<num; i++) {
arr[i] = sc.nextInt();
}
for(int i=0; i<num; i++) {
System.out.println(arr[i]);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for(int i =0; i<num; i++) {
System.out.println(sc.nextInt());
}
}
}
1073 : 0 입력될 때까지 무한 출력하기2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while(true) {
int a = sc.nextInt();
if(a != 0)
System.out.println(a);
else
break;
}
}
}
1074 : 정수 1개 입력받아 카운트다운 출력하기1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for(int i =0; i<num; i++) {
System.out.println(num-i);
}
}
}
1075 : 정수 1개 입력받아 카운트다운 출력하기2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for(int i =0; i<num; i++) {
System.out.println(num-i-1);
}
}
}
1076 : 문자 1개 입력받아 알파벳 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
char a = sc.next().charAt(0);
char b = 'a';
while(b <= a) {
System.out.printf("%c ", b);
b++;
}
}
}
1077 : 정수 1개 입력받아 그 수까지 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for(int i=0; i <=a; i++) {
System.out.println(i);
}
}
}
1078 : 짝수 합 구하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int sum = 0;
for(int i=1; i <=a; i++) {
if(i%2==0) sum+=i;
}
System.out.println(sum);
}
}
1079 : 원하는 문자가 입력될 때까지 반복 출력하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while(true) {
String a = sc.next();
if(a.equals("q")) {
System.out.println(a);
break;
} else
System.out.println(a);
}
}
}
- char로 해도 무방
1080 : 언제까지 더해야 할까?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int max_value = sc.nextInt();
int sum = 0;
int value = 1;
while (true) {
sum += value;
if (sum > max_value) {
System.out.println(value-1);
break;
}
value++;
}
}
}
1081 : 주사위를 2개 던지면?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a1 = sc.nextInt();
int a2 = sc.nextInt();
for(int i=0; i<a1; i++) {
for(int j=0; j<a2; j++) {
System.out.printf("%d %d\n", i+1, j+1);
}
}
}
}
1082 : 16진수 구구단?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(16);
for(int i=1; i < 16; i++) {
System.out.printf("%X * %X = %X\n", a, i, a*i);
}
}
}
- nextInt()안의 인자를 통해 해당 진수로 저장 가능
- %x : 소문자로 16진수 출력 / %X : 대문자로 16진수 출력
1083 : 3 6 9 게임의 왕이 되자!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for(int i=1; i<=a; i++) {
if((i==3) || (i==6) || (i==9))
System.out.print("X ");
else
System.out.print(i + " ");
}
}
}
1084 : 빛 섞어 색 만들기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int g = sc.nextInt();
int b = sc.nextInt();
int count = 0;
for(int i=0; i<r; i++) {
for(int j=0; j<g; j++) {
for(int k=0; k<b; k++) {
System.out.println(i + " " + j + " " + k);
count++;
}
}
}
System.out.println(count);
}
}
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] a = br.readLine().split(" ");
int count = 0;
for(int i=0; i<Integer.valueOf(a[0]); i++) {
for(int j=0; j<Integer.valueOf(a[1]); j++) {
for(int k=0; k<Integer.valueOf(a[2]); k++) {
bw.write(i + " " + j + " " + k + "\n");
count++;
}
}
}
// bw.write(count); 정수를 그대로 출력 불가, String으로 바꿔줘야
bw.write(String.valueOf(count));
bw.flush();
}
}
- Scanner -> BufferedReader & Writer로 구현
- bw.write(int) -> 출력되지 않음, String.valueOf를 통해 String으로 바꾼 후 출력하자.
- BufferedReader는 한줄을 통째로 받는 입력이다. 따라서 스캐너에 비해서 훨씬 성능이 높다.
- 시간초과 시 BufferedReader로 바꿔서 제출하자.
1085 : 소리 파일 저장용량 계산하기
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int h = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int s = sc.nextInt();
float total = h*b*c*s;
float result = ((total) / 8/1024/1024);
System.out.printf("%.1f MB\n", result);
}
}
- int 형태로 계산 후 (float)형변환 하면, 이미 int에서 깎이는 부분이 있으므로 정상 출력되지 않는다.
- 따라서 float를 선언하고 하자.
1086 : 그림 파일 저장용량 계산하기
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int w = sc.nextInt();
int h = sc.nextInt();
int b = sc.nextInt();
double mul = w*h*b;
double result = (mul /8/1024/1024);
System.out.printf("%.2f MB\n", result);
}
}
1087 : 여기까지! 이제 그만~
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int num = 1;
int sum = 0;
while(true) {
sum += num;
if(sum >= a) {
System.out.println(sum);
break;
}
num ++;
}
}
}
1088 : 3의 배수는 통과?
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int num = 1;
while(num <= a) {
if(num % 3 == 0) {
num++;
continue;
}
System.out.print(num + " ");
num++;
}
}
}
1089 : 수 나열하기1
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int d = sc.nextInt();
int n = sc.nextInt();
int result = a+d*(n-1);
System.out.println(result);
}
}
1090 : 수 나열하기2
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int d = sc.nextInt();
int n = sc.nextInt();
long result = (long)(a*(Math.pow(d, n-1)));
System.out.println(result);
}
}
1091 : 수 나열하기3
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int m = sc.nextInt();
int d = sc.nextInt();
int n = sc.nextInt();
long result = a;
for(int i=1; i<n; i++) {
result = (result*m)+d;
}
System.out.println(result);
}
}
1092 : 함께 문제 푸는 날
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int day = 1;
while (true) {
if ((day % a != 0) || (day % b != 0) || (day % c != 0))
day++;
else
break;
}
System.out.println(day);
}
}
1093 : 이상한 출석 번호 부르기1
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int[] arr = new int[23];
for(int i = 0; i<a; i++) {
int num = sc.nextInt();
arr[num - 1]++;
}
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
1094 : 이상한 출석 번호 부르기2
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int[] arr = new int[a];
for(int i = 0; i<a; i++) {
arr[i] = sc.nextInt();
}
for(int i=a-1; i>=0; i--) {
System.out.print(arr[i] + " ");
}
}
}
1095 : 이상한 출석 번호 부르기3
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int[] arr = new int[a];
int min = 24;
for (int i = 0; i < a; i++) {
arr[i] = sc.nextInt();
if (arr[i] < min) {
min = arr[i];
}
}
System.out.println(min);
}
}
1096 : 바둑판에 흰 돌 놓기
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int[][] arr = new int[19][19];
int num = sc.nextInt();
for(int i=0; i<num; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
arr[x-1][y-1] = 1;
}
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
1097 : 바둑알 십자 뒤집기
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int[][] arr = new int[19][19];
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr.length; j++) {
arr[i][j] = sc.nextInt();
}
}
int num = sc.nextInt();
for(int i=0; i<num; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
for(int j=0; j < arr.length; j++) {
if(arr[x-1][j] == 0) {
arr[x-1][j] = 1;
} else
arr[x-1][j] = 0;
if(arr[j][y-1] == 0) {
arr[j][y-1] = 1;
} else
arr[j][y-1] = 0;
}
}
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
1098 : 설탕 과자 뽑기
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int[][] arr = new int[sc.nextInt()][sc.nextInt()];
int n = sc.nextInt(); // 막대 수
for(int i = 0; i < n; i++) {
int length = sc.nextInt();
int direc = sc.nextInt();
int h = sc.nextInt();
int w = sc.nextInt();
if(direc == 0) {
for(int j = 0; j < length; j++) {
arr[h-1][w-1+j] = 1;
}
} else {
for(int j = 0; j < length; j++) {
arr[h-1+j][w-1] = 1;
}
}
}
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
1099 : 성실한 개미
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
public class HelloWorldMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int[][] arr = new int[10][10];
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr.length; j++) {
arr[i][j] = sc.nextInt();
}
}
int x = 1, y = 1;
arr[x][y] = 9;
while(true) {
if(arr[x][y+1] != 1) {
y++;
if((x < 10 && y < 10) && arr[x][y] != 2)
arr[x][y] = 9;
else
break;
} else {
x++;
if((x < 10 && y < 10) && arr[x][y] != 2)
arr[x][y] = 9;
else {
arr[x][y] = 9;
break;
}
}
}
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
'알고리즘' 카테고리의 다른 글
[백준] 13305 - 주유소 (자바) (0) | 2021.05.01 |
---|---|
[백준] 1541 - 잃어버린 괄호 (자바) (0) | 2021.04.30 |
[백준] 1931 - 회의실 배정 (자바) (0) | 2021.04.30 |
[백준] 11399 - ATM (자바) (0) | 2021.04.30 |
[백준] 11047 - 동전0 (자바) (0) | 2021.04.30 |