전.java
[Java] 공부시간
jeonnew
2023. 1. 31. 17:01
728x90
- 공부 시간 반환
- 5분 이상 인정
- 1시간 45분 이상은 1시간 45분으로 인정
(입력)
["08:30", "09:00", "14:00", "16:00", "16:01", "16:06", "16:07", "16:11"]
["01:00", "08:00", "15:00", "15:04", "23:00", "23:59"]
(출력)
# 시작, 끝 시작을 한 쌍으로 생각
public class Main {
private String solution(String[] log) {
int total = 0;
String result = "";
for(int i=0; i<log.length; i+=2) {
int start = Integer.parseInt(log[i].split(":")[0])*60 + Integer.parseInt(log[i].split(":")[1]);
int end = Integer.parseInt(log[i+1].split(":")[0])*60 + Integer.parseInt(log[i+1].split(":")[1]);
int time = end-start;
if(time >= 5 && time < 105) total += time;
else if(time >= 105) total += 105;
}
int h = total/60;
int m = total%60;
if(h<10) result+="0"+h;
else result+=h;
if(m<10) result+="0"+m;
else result+=m;
return result;
}
public static void main(String[] args) {
Main T = new Main();
String[] s = new String[]{"08:30", "09:00", "14:00", "16:00", "16:01", "16:06", "16:07", "16:11"};
String[] s2 = new String[]{"01:00", "08:00", "15:00", "15:04", "23:00", "23:59"};
System.out.println(T.solution(s)); // 02:20
System.out.println(T.solution(s2)); // 02:44
}
}
# ArrayList 사용
import java.util.ArrayList;
import java.util.Collections;
public class Main {
private String solution(String[] log) {
ArrayList<Integer> tmp = new ArrayList<>();
int total = 0;
String result = "";
for(int i=0; i<log.length; i++) {
tmp.add(Integer.parseInt(log[i].split(":")[0])*60 + Integer.parseInt(log[i].split(":")[1]));
}
for(int i=0; i<tmp.size(); i+=2) {
int time = tmp.get(i+1) - tmp.get(i);
if(time < 5) total += 0;
else if(time >= 105) total += 105;
else total += time;
}
int h = total/60;
int m = total%60;
result = (h<10?"0"+h:h)+":"+(m<10?"0"+m:m);
return result;
}
public static void main(String[] args) {
Main T = new Main();
String[] s = new String[]{"08:30", "09:00", "14:00", "16:00", "16:01", "16:06", "16:07", "16:11"};
String[] s2 = new String[]{"01:00", "08:00", "15:00", "15:04", "23:00", "23:59"};
System.out.println(T.solution(s)); // 02:20
System.out.println(T.solution(s2)); // 02:44
}
}
- 삼항 연산자 사용( ? : ; )
# 휴..
- 파이썬이랑 자바랑 자바스크립트 문법이 자꾸 꼬인다
반응형