2024.03.28
코딩테스트
[SWEA] JAVA 2단계
▶ 스도쿠 검증 [SWEA 1974]
import java.io.*;
import java.util.*;
// 일반적인 스도쿠
// 행, 열, 칸(3*3)을 모두 만족할 시 1을 출력, 하나라도 만족하지 않을 경우 0을 출력
class Solution {
public static void main(String args[]) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //읽는 라인
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); //출력 라인
int T;
T= Integer.parseInt(bf.readLine());
for(int test_case = 1; test_case <= T; test_case++) {
int[][] arr = new int[9][9];
int result = 1;
// 9*9 숫자 입력
for(int i = 0; i < 9; i++){
String s = bf.readLine();
StringTokenizer st = new StringTokenizer(s);
String sArr[] = s.split(" ");
for(int j = 0; j < 9; j++){
arr[i][j] = Integer.parseInt(sArr[j]);
}
}
// 행, 열 검증
for(int i = 0; i < 9; i++) {
int rowSum = 0;
int colSum = 0;
for(int j = 0; j < 9; j++){
rowSum += arr[i][j];
colSum += arr[j][i];
}
if (rowSum != 45 || colSum != 45) {
result = 0;
break;
}
}
if( result == 0) {
bw.write(String.valueOf("#" + test_case + " " + result));
bw.newLine();
continue;
}
// 칸(3*3) 검증
for (int i = 1; i <= 3; i++) {
int cellSum = 0;
for(int j = (i - 1) * 3; j < i*3; j++){
for(int k = (i - 1) * 3; k < i*3; k++){
cellSum += arr[j][k];
}
}
if (cellSum != 45) {
result = 0;
break;
}
if (result == 0) { break;}
}
bw.write(String.valueOf("#" + test_case + " " + result));
bw.newLine();
}
bw.flush();
bw.close();
}
}
3*3 칸 검증 시 3중 for문을 사용하였지만, 그다지 좋은 방법은 아닌 것 같다.
▶ 시각 덧셈 [SWEA 1976]
import java.util.*;
import java.io.*;
// 시 분으로 이루어진 시각 2개를 입력 받아, 더한 값을 시 분으로 출력하는 프로그램
// 시는 1~12, 분은 0~59의 정수를 가질 수 있다.
class Solution {
public static void main(String args[]) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //읽는 라인
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); //출력 라인
Scanner sc = new Scanner(System.in);
int T;
T=Integer.parseInt(bf.readLine());
for(int test_case = 1; test_case <= T; test_case++) {
String s = bf.readLine();
StringTokenizer st = new StringTokenizer(s);
String arr[] = s.split(" ");
int firH = Integer.parseInt(arr[0]);
int firM = Integer.parseInt(arr[1]);
int secH = Integer.parseInt(arr[2]);
int secM =Integer.parseInt(arr[3]);
int totalH = 0;
int totalM = 0;
totalH = firH + secH;
totalM = firM + secM;
if (totalH > 12) { // 합산한 시가 12 초과일 경우 -12를 한다.
totalH = totalH - 12;
}
if (totalM > 59) { // 합산한 분이 59 초과일 경우 분에서 -60을 한 후, 시에 1을 더한다.
totalM = totalM -60;
totalH += 1;
}
bw.write(String.valueOf("#" + test_case + " " + totalH + " " + totalM));
bw.newLine();
}
bw.flush();
bw.close();
}
}
'study' 카테고리의 다른 글
study - day11 (0) | 2024.04.03 |
---|---|
study - day10 (0) | 2024.04.01 |
study - day08 (0) | 2024.04.01 |
study - day07 (1) | 2024.03.25 |
study - day02 (0) | 2024.03.13 |