public static int[] get_num(int num){
int left = (int) Math.floor(num/10);
int right = num % 10;
return new int[]{left,right};
}
public static int cycle(int left, int right) {
int[] l_r = get_num(left+right);
String res = String.valueOf(right) + l_r[1];
return Integer.valueOf(res);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int res = 0, count = 1;
int[] l_r = get_num(n);
while(true){
res = cycle(l_r[0], l_r[1]);
if(res == n) break;
count++;
l_r = get_num(res);
}
System.out.println(count);
}
그냥 구현 문제다.
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void write(String s) throws IOException {
bw.write(s);
bw.newLine();
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
LinkedList<Integer> list = new LinkedList<>();
while (t > 0) {
String[] cmd = br.readLine().split(" +");
if (cmd[0].equals("push")) list.addFirst(Integer.parseInt(cmd[1]));
if (cmd[0].equals("pop")) {
if (list.isEmpty()) write("-1");
else write(String.valueOf(list.pollFirst()));
}
if (cmd[0].equals("size")) write(String.valueOf(list.size()));
if (cmd[0].equals("empty")) {
if (list.isEmpty()) write("1");
else write("0");
}
if (cmd[0].equals("top")) {
if (list.isEmpty()) write("-1");
else write(String.valueOf(list.getFirst()));
}
t--;
}
bw.flush();
bw.close();
}
출력에 buffer를 사용하지 않으면 통과되지 않았다.
Collection을 사용하지 않고 Array로 구현하는게 가장 빠를 것이다.
Collection을 사용한다면 입출력이 주 동작이므로 LinkedList가 더 적합하다.
책에선 조회가 많으면 ArrayList
삽입/삭제가 많으면 LinkedList
라고 하긴 했지만 검색해보면 별반 차이 없는 듯 하다.
resize만 적당히 고려해서 ArrayList 사용해도 좋아 보인다.