문제 1
더보기
import java.util.*;
public class Main {
// 특정한 시각 안에 '3'이 포함되어 있는지의 여부
public static boolean check(int h, int m, int s) {
// h의 10의 자리는 0,1,2 밖에 없으므로 확인할 필요 없음
if (h % 10 == 3 || m / 10 == 3 || m % 10 == 3 || s / 10 == 3 || s % 10 == 3)
return true;
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// H를 입력받기
int h = sc.nextInt();
int cnt = 0;
for (int i = 0; i <= h; i++) {
for (int j = 0; j < 60; j++) {
for (int k = 0; k < 60; k++) {
// 매 시각 안에 '3'이 포함되어 있다면 카운트 증가
if (check(i, j, k)) cnt++;
}
}
}
System.out.println(cnt);
}
}
문제 2
더보기
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 현재 나이트의 위치 입력받기
String inputData = sc.nextLine();
int row = inputData.charAt(1) - '0';
int column = inputData.charAt(0) - 'a' + 1;
// 나이트가 이동할 수 있는 8가지 방향 정의
int[] dx = {-2, -1, 1, 2, 2, 1, -1, -2};
int[] dy = {-1, -2, -2, -1, 1, 2, 2, 1};
// 8가지 방향에 대하여 각 위치로 이동이 가능한지 확인
int result = 0;
for (int i = 0; i < 8; i++) {
// 이동하고자 하는 위치 확인
int nextRow = row + dx[i];
int nextColumn = column + dy[i];
// 해당 위치로 이동이 가능하다면 카운트 증가
if (nextRow >= 1 && nextRow <= 8 && nextColumn >= 1 && nextColumn <= 8) {
result += 1;
}
}
System.out.println(result);
}
}
내가 풀어본 코드
import java.util.*;
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String pos = sc.nextLine();
char x = pos.charAt(0);
int y = Integer.parseInt(pos.substring(1,2));
int count = 0;
// 1. 수평으로 두 칸 이동한 뒤 수직으로 한 칸 이동
// 수평이동 왼쪽
char nx = (char) (x - 2);
int ny = y + 1;
if ('a' <= nx && nx <= 'h' && 1<= ny && ny <= 8) {
count += 1;
}
ny = y - 1;
if ('a' <= nx && nx <= 'h' && 1<= ny && ny <= 8) {
count += 1;
}
// 수평이동 오른쪽
nx = (char) (x + 2);
ny = y + 1;
if ('a' <= nx && nx <= 'h' && 1<= ny && ny <= 8) {
count += 1;
}
ny = y - 1;
if ('a' <= nx && nx <= 'h' && 1<= ny && ny <= 8) {
count += 1;
}
// 2. 수직으로 두 칸 이동한 뒤 수평으로 한 칸 이동
// 수직이동 아래쪽
ny = y + 2;
nx = (char) (x - 1);
if ('a' <= nx && nx <= 'h' && 1<= ny && ny <= 8) {
count += 1;
}
nx = (char) (x + 1);
if ('a' <= nx && nx <= 'h' && 1<= ny && ny <= 8) {
count += 1;
}
// 수직이동 위쪽
ny = y - 2;
nx = (char) (x - 1);
if ('a' <= nx && nx <= 'h' && 1<= ny && ny <= 8) {
count += 1;
}
nx = (char) (x + 1);
if ('a' <= nx && nx <= 'h' && 1<= ny && ny <= 8) {
count += 1;
}
System.out.println(count);
}
}
문제 3
더보기
import java.util.*;
public class Main {
public static String str;
public static ArrayList<Character> result = new ArrayList<Character>();
public static int value = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
str = sc.next();
// 문자를 하나씩 확인하며
for (int i = 0; i < str.length(); i++) {
// 알파벳인 경우 결과 리스트에 삽입
if (Character.isLetter(str.charAt(i))) {
result.add(str.charAt(i));
}
// 숫자는 따로 더하기
else {
value += str.charAt(i) - '0';
}
}
// 알파벳을 오름차순으로 정렬
Collections.sort(result);
// 알파벳을 차례대로 출력
for (int i = 0; i < result.size(); i++) {
System.out.print(result.get(i));
}
// 숫자가 하나라도 존재하는 경우 가장 뒤에 출력
if (value != 0) System.out.print(value);
System.out.println();
}
}
내 코드
import java.util.*;
import java.io.*;
class Main {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
char[] sarr = new char[s.length()];
String result = "";
int sum = 0;
for(int i=0; i<s.length(); i++) {
sarr[i] = s.charAt(i);
}
Arrays.sort(sarr);
for(int i=0; i<s.length(); i++) {
if('A'<=sarr[i] && sarr[i]<='Z') {
result += sarr[i];
} else {
sum += sarr[i] - '0';
}
}
result += sum;
System.out.println(result);
}
}
이 경우 입력된 문자열에 숫자가 없을 경우에도 결괏값에는 마지막에 0이 들어간다. 따라서 sum이 0일 경우 result에서 제외시키는 코드가 있어야 한다.
import java.util.*;
import java.io.*;
class Main {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
char[] sarr = new char[s.length()];
String result = "";
int sum = 0;
for(int i=0; i<s.length(); i++) {
sarr[i] = s.charAt(i);
}
Arrays.sort(sarr);
for(int i=0; i<s.length(); i++) {
if('A'<=sarr[i] && sarr[i]<='Z') {
result += sarr[i];
} else {
sum += sarr[i] - '0';
}
}
if(!(sum==0)) {
result += sum;
}
System.out.println(result);
}
}