자바 질문드립니다!!
조회수 288회
아래와 같이 Block 이 주어진 상황에서 isValidAdvanced(콜라츠 정의)를 풀어야 합니다 ㅜㅜ
테스트가 임의로 주어지고 그 테스트에 제가 만든 메소드를 돌렸을 때 원하는 값이 출력이 돼야하는 형식이에요!
Block 에 id,rank, null 이 있고 id는 그냥 숫자, rank 는 1,2,3 차례대로 올라갑니다!
반복문은 사용하면 안 되고 재귀함수만 사용해서 풀어야 해요 ㅜㅜ
next는 다음 Block 을 가르켜요 혹시 예시를 위해 rank 를 신경쓰지 않은 isvalid 를 같이 적어 놓았습니다!!
감사합니다!!
public class Block { public int id; // Number public int rank; // position in the Chain public Block next;
// attributes to be added
public int size;
public Block(int id, int rank) {
this.id = id;
this.rank = rank;
this.next = null;
// attributes to be added
this.size = 0;
}
public Block(int id, int rank, Block next) {
this.id = id;
this.rank = rank;
this.next = next;
// attributes to be added
this.size = 0;
}
public boolean isValid() {
if(this.id == 1 && this.next == null) {
return true;
}
else {
if(this.id%2 == 0) {
if(this.next.id == 1 && this.next.next != null) {
return false;
}
if(this.next.id == 1) {
return true;
}
if(this.next.id != 1 && this.next.next == null) {
return false;
}
if(this.id/2 != this.next.id) {
return false;
}
return this.next.isValid();
}
else {
if(this.next.id%2!=0) {
return false;
}
if(this.id*3+1 != this.next.id) {
return false;
}
else {
return this.next.isValid();
}
}
}
}
/**
* IMPORTANT: Even thought this method is in the Block class, it may be harder
* than most in the Chain class.
*
* @return true if the chain starting at the current node is either a valid
* Collatz chain or a fast Collatz chain. In this question, you also
* need to ensure that the rank is valid. A valid rank starts at 1 and
* increases with one.
*/
public boolean isValidAdvanced() {
}
댓글 입력