-
카테고리
-
세부 분야
알고리즘 · 자료구조
-
해결 여부
미해결
5-6 공주구하기
22.10.17 14:47 작성 조회수 227
0
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public int solution(int n,int m) {
int answer= 0;
Queue<Integer> qu = new LinkedList<>();
for(int i=0; i<n; i++) {
qu.offer(i);
}
while(qu.size()!=1) {
for(int i=0; i<m-1; i++) {
qu.poll();
if(qu.size()==1) { break;
}else{
qu.offer(qu.poll());
}
}
}
answer = qu.peek();
return answer }
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
Main T = new Main();
int N = sc.nextInt();
int M = sc.nextInt();
System.out.println(T.solution(N,M));
}
}
예제를 입력하면 맞다고 나오는데 계속 수정해도 채점 사이트에서는 틀리다고 나와서 어느 부분에서 틀린지 잘모르겠어요 ㅠ.ㅠ
답변을 작성해보세요.
0
김태원
지식공유자2022.10.20
안녕하세요^^
return answer 뒤에 세미콜론이 없습니다.
코드 수정해보았습니다.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public int solution(int n,int m) {
int answer= 0;
Queue<Integer> qu = new LinkedList<>();
for(int i=1; i<=n; i++) {
qu.offer(i);
}
while(qu.size()!=1) {
for(int i=0; i<m-1; i++) {
qu.offer(qu.poll());
}
qu.poll();
if(qu.size()==1) break;
}
answer = qu.peek();
return answer; }
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
Main T = new Main();
int N = sc.nextInt();
int M = sc.nextInt();
System.out.println(T.solution(N,M));
}
}
답변 1