이번 주에 회사 팀원들과 회식을 했다. 오랜만에 소고기를 먹었더니 기분이 좋았다. 요즘 회사 일에 회의를 느끼고 있었다. 그래서 일에 집중도 못 하고 불안불안한 상태로 회사에 다니고 있다. 그런데 회식을 기회로 회사 분들과 이런저런 이야기를 하면서 많은 위로를 받았다. 이렇게 친절한 사람들이라니! 감사한 분들이다.
위의 백준 1244번 문제 제출 코드이다. 뭐가 문제인지 고민 중이다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Solution s = new Solution();
System.out.println(s.solution(getLightBalls(br), getStudents(br)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static int[] getLightBalls(BufferedReader br) throws IOException {
int len = Integer.parseInt(br.readLine());
int[] lightBalls = new int[len + 1];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 1; i < lightBalls.length; i++) {
lightBalls[i] = Integer.parseInt(st.nextToken());
}
return lightBalls;
}
private static int[][] getStudents(BufferedReader br) throws IOException {
int len = Integer.parseInt(br.readLine());
int[][] students = new int[len][2];
for (int i = 0; i < len; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int sex = Integer.parseInt(st.nextToken());
int target = Integer.parseInt(st.nextToken());
students[i][0] = sex;
students[i][1] = target;
}
return students;
}
}
class Solution {
public String solution(int[] lightBalls, int[][] students) {
Calculator c = new Calculator(lightBalls, students);
return c.getLightBalls();
}
class Calculator {
int[] lightBalls;
int[][] students;
public Calculator(int[] lightBalls, int[][] students) {
this.lightBalls = lightBalls;
this.students = students;
}
public String getLightBalls() {
calc();
StringBuilder result = new StringBuilder();
for (int i = 1; i < this.lightBalls.length; i++) {
result.append(this.lightBalls[i]);
if(i % 20 == 0) result.append("\n");
else if(i != this.lightBalls.length - 1) result.append(" ");
}
return result.toString();
}
private void calc() {
for (int[] student : students) {
if(student[0] == 1) maleOper(student[1]);
else femaleOper(student[1]);
}
}
private void maleOper(int target) {
for (int i = target; i < this.lightBalls.length; i+= target) {
switchLightBall(i);
}
}
private void femaleOper(int target) {
switchLightBall(target);
int widening = 1;
while (isConditionMetLightBalls(target - widening, target + widening)) {
switchLightBall(target - widening);
switchLightBall(target + widening);
widening++;
}
}
private boolean isConditionMetLightBalls(int x, int y) {
return 0 <= x && x < this.lightBalls.length && 0 <= y && y < this.lightBalls.length && this.lightBalls[x] == this.lightBalls[y];
}
private void switchLightBall(int target) {
this.lightBalls[target] = this.lightBalls[target] == 1 ? 0 : 1;
}
}
}
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args){
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
Input ip = getInput(br);
Solution s = new Solution();
System.out.println(s.solution(ip.nums, ip.target));
}catch(IOException e){
throw new RuntimeException(e);
}
}
private static Input getInput(BufferedReader br) throws IOException{
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int len = Integer.parseInt(st.nextToken());
int target = Integer.parseInt(st.nextToken());
int[] nums = new int[len];
st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i < len; i++){
nums[i] = Integer.parseInt(st.nextToken());
}
return new Input(nums, target);
}
}
class Solution{
public int solution(int[] nums, int target){
Arrays.sort(nums);
return nums[target - 1];
}
}
class Input{
int[] nums;
int target;
public Input(int[] nums, int target){
this.nums = nums;
this.target = target;
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Solution s = new Solution();
System.out.println(s.solution(getInput(br)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static int getInput(BufferedReader br) throws IOException {
return Integer.parseInt(br.readLine());
}
}
class Solution {
public String solution(int target) {
return getValueOfIndexedTarget(getIndexedLst(target), target);
}
private List<Integer> getIndexedLst(int target) {
List<Integer> lst = new ArrayList<>();
int input = 1;
int differ = 1;
while (input <= target) {
lst.add(input);
input += differ;
differ++;
}
return lst;
}
private String getValueOfIndexedTarget(List<Integer> indexedList, int target) {
int lastElem = indexedList.get(indexedList.size() - 1);
int lastIndex = indexedList.indexOf(lastElem);
return getValue(lastIndex, target - lastElem);
}
private String getValue(int lastIndex, int diff) {
if(lastIndex % 2 == 0) return getEvenIndexValue(lastIndex, diff);
return getOddIndexValue(lastIndex, diff);
}
private String getEvenIndexValue(int lastIndex, int diff) {
int child = lastIndex + 1;
int parent = 1;
for (int i = 0; i < diff; i++) {
child--;
parent++;
}
return child + "/" + parent;
}
private String getOddIndexValue(int lastIndex, int diff) {
int child = 1;
int parent = lastIndex + 1;
for (int i = 0; i < diff; i++) {
child++;
parent--;
}
return child + "/" + parent;
}
}