Ducci+Sequence

Source Code Java code format="java5" import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner;

public class Ducci { public static void main(String[] args) { Scanner scan = new Scanner(System.in);

int T = scan.nextInt;

for(int i = 0; i < T; i++) { int n = scan.nextInt;   // 테스트 케이스의 갯수

int[] nums = new int[n]; for(int j = 0; j < n; j++) { nums[j] = scan.nextInt; }           Tuple tuple = new Tuple(nums);

List sequence = new ArrayList;

while(true) { if(tuple.isAllZero) { System.out.println("ZERO"); break; }

tuple = tuple.createNext;

if(sequence.contains(tuple)) { System.out.println("LOOP"); break; }

sequence.add(tuple); }       }    } }

class Tuple { private int[] nums;

public Tuple(int... nums) { this.nums = nums; }

public Tuple createNext { int[] newTuples = new int[nums.length];

for(int i = 1; i < nums.length; i++) { newTuples[i - 1] = Math.abs(nums[i - 1] - nums[i]); }       newTuples[nums.length - 1] = Math.abs(nums[nums.length - 1] - nums[0]);

return new Tuple(newTuples); }

// 튜플의 모든 수가 0 인지 확인한다. public boolean isAllZero { for(int i = 0; i < nums.length; i++) { if(nums[i] != 0) { return false; }       }        return true; }

@Override public boolean equals(Object obj) { if(obj instanceof Tuple) { Tuple newObj = (Tuple) obj;

for(int i = 0; i < nums.length; i++) { if(nums[i] != newObj.nums[i]) { return false; }           }            return true; }       return false; }

@Override public String toString { return Arrays.toString(nums); } } code

Comment