[코드업] 코드업 기초 100문제 풀이 1011 ~ 1020 (Java)
# 1011 - 문자 1개 입력받아 그대로 출력하기
입력
문자 1개가 입력된다.
(단, 입력되는 문자는 알파벳 대, 소문자 및 숫자를 비롯한 아스키코드로 표현할 수 있는 문자들만 입력된다.)
출력
입력된 문자를 그대로 출력한다.
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
System.out.println(input);
}
}
# 1012 - 실수 1개 입력받아 그대로 출력하기
입력
실수값 1개가 입력된다.
(단, float로 저장할 수 있는 실수 값만 입력된다.)
출력
입력된 실수값을 그대로 출력한다.
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
float x = sc.nextFloat();
System.out.printf("%.6f", x);
}
}
처음에 System.out.println(x) 라고 했을 때 소숫점 아래 6자리까지 표시하라고 하여
System.out.printf("%.6f", x)로 바꿔줬음
# 1013 - 정수 2개 입력받아 그대로 출력하기
입력
2개의 정수가 공백으로 구분되어 입력된다.
출력
입력된 두 정수를 공백으로 구분하여 출력한다.
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);
System.out.println(b);
}
}
# 1014 - 문자 2개 입력받아 순서 바꿔 출력하기
입력
2개의 문자가 공백으로 구분되어 입력된다.
출력
두 문자의 순서를 바꿔 출력한다.
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
System.out.println(b);
System.out.println(a);
}
}
# 1015 - 실수 입력받아 둘째 자리까지 출력하기
입력
실수 1개가 입력된다.
출력
결과를 소수점 셋 째 자리에서 반올림 해, 소숫점 이하 둘째 자리까지 출력한다.
(%.2f를 이용하면 된다.)
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
float x = sc.nextFloat();
System.out.printf("%.2f", x);
}
}
# 1017 - 정수 1개 입력받아 3번 출력하기
입력
정수 1개가 입력된다.
출력
입력받은 정수를 공백으로 구분해 3번 출력한다.
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+" "+a+" "+a);
}
}
# 1018 - 시간 입력받아 그대로 출력하기
입력
시(hour)와 분(minute)이 ":" 으로 구분되어 입력된다.
출력
입력받은 시간을 "시:분" 형식으로 출력한다.
import java.util.Scanner;
public class Main{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
System.out.println(a);
}
}
문자열이라고 생각하고 그대로 출력했음
# 1019 - 연월일 입력받아 그대로 출력하기
입력
연, 월, 일이 ".(닷)"으로 구분되어 입력된다.
출력
입력받은 연, 월, 일을 yyyy.mm.dd 형식으로 출력한다.
(%02d를 사용하면 2칸을 사용해 출력하는데, 한 자리 수인 경우 앞에 0을 붙여 출력한다.)
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String x = sc.next();
String date[] = x.split("[.]");
// split()을 이용해 .(닷)을 기준으로 문자열을 잘라 배열에 저장
int year = Integer.parseInt(date[0]);
int month = Integer.parseInt(date[1]);
int day = Integer.parseInt(date[2]);
System.out.printf("%04d.%02d.%02d", year, month, day);
}
}
'.(닷)'을 기준으로 나눌 때 ' [.] ' or ' \\. ' 으로 처리함
# 1020 - 주민번호 입력받아 형태 바꿔 출력하기
입력
주민번호 앞 6자리와 뒷 7자리가 '-'로 구분되어 입력된다.
(입력값은 가상의 주민번호이다.)
ex)110011-0000000
출력
'-'를 제외한 주민번호 13자리를 모두 붙여 출력한다.
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String x = sc.next();
// 풀이 1번
String registration[] = x.split("[-]");
int birth = Integer.parseInt(registration[0]);
int security = Integer.parseInt(registration[1]);
System.out.printf("%06d%07d", birth, security);
// 풀이 2번
x = x.replaceAll("-", "");
System.out.println(x);
}
}
print로 출력할 때 자릿수 설정을 안해주면
ex) ' 000911-2222222 ' 을 입력하면 ' 9112222222 ' 이렇게 앞에 0을 생략하고 출력하면 오류가 뜸