fork download
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.StringTokenizer;
  7.  
  8. public class Main {
  9.  
  10. private static class FastScanner {
  11. String next() throws IOException {
  12. while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
  13. return st.nextToken();
  14. }
  15. int nextInt() throws IOException { return Integer.parseInt(next()); }
  16. }
  17.  
  18. private static List<int[]> build(int n) {
  19. List<int[]> ops = new ArrayList<>();
  20. int l = 1, r = n;
  21. for (int row = 2; row <= n; row++) {
  22. if ((row & 1) == 0) {
  23. ops.add(new int[]{row, l, r});
  24. ++l;
  25. } else {
  26. ops.add(new int[]{row, l, r - 1});
  27. ops.add(new int[]{row, l + 1, r});
  28. --r;
  29. }
  30. }
  31. return ops;
  32. }
  33.  
  34. private static void output(List<int[]> ops, StringBuilder sb) {
  35. sb.append(ops.size()).append('\n');
  36. for (int[] o : ops) sb.append(o[0]).append(' ').append(o[1]).append(' ').append(o[2]).append('\n');
  37. }
  38.  
  39. public static void main(String[] args) throws Exception {
  40. FastScanner fs = new FastScanner();
  41. int t = fs.nextInt();
  42. StringBuilder ans = new StringBuilder();
  43. while (t-- > 0) {
  44. int n = fs.nextInt();
  45. List<int[]> ops = build(n);
  46. output(ops, ans);
  47. }
  48. System.out.print(ans.toString());
  49. }
  50. }
  51.  
Success #stdin #stdout 0.11s 54976KB
stdin
2
3
4
stdout
3
2 1 3
3 2 2
3 3 3
4
2 1 4
3 2 3
3 3 4
4 2 3