SWEA 1225暗号生成器



キューを利用して、poll()の値を条件に従って減らした後、offer()を行います.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Solution {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;
	Queue<Integer> q;
	
	public static void main(String[] args) throws IOException {
		
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = 10;
		for(int tc = 1; tc <= T; tc++) {
			br.readLine();
			st = new StringTokenizer(br.readLine(), " ");
			Queue<Integer> q = new LinkedList<>();
			for(int i = 0; i < 8; i++) {
				q.offer(Integer.parseInt(st.nextToken()));
			}
			int i = 1;
			while(true) {
				int tmp = q.poll() - i++;
				if(i > 5) i = 1;
				if(tmp <= 0) {
					tmp = 0;
					q.offer(tmp);
					break;
				}else {
					q.offer(tmp);
				}
			}
			
			bw.write(String.format("#%d", tc));
			for(int x : q) {
				bw.write(String.format(" %d", x));
			}
			bw.write("\n");
			
		}
		bw.flush();
		bw.close();
	}
	
}