神秘失踪换行

Mysterious Missing Newline

我正在开发一个旨在解决位于以下站点的挑战性问题的程序。 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=919

我相信我已经正确地解决了这个问题。我需要以特定方式打印出我的答案。对于几乎所有的测试输入,我的程序都以正确的方式打印出响应。但是,对于以下站点提供的输入,我得到了一个错误。 http://www.udebug.com/UVa/978

我在第 137383 行周围缺少一个换行符。

我应该有:

1
1
1

green wins

我得到:

1
1
1
green wins

我已经 运行 梳理了我的代码,但无法弄清楚为什么会发生这种情况。它似乎没有发生在其他任何地方,即使是类似的输入。

代码如下:

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Lem4 {
    static Comparator<Integer> c = new reverseC();
    static PriorityQueue<Integer> green = new PriorityQueue<Integer>(100002, c);
    static PriorityQueue<Integer> blue = new PriorityQueue<Integer>(100002, c);
    static int bf[][] = new int[100048][2];

    public static void main(String[] args) {
        Scanner in = new Scanner((System.in));
        int count = in.nextInt();
        StringBuilder t = new StringBuilder();
        while (count-- > 0) {
            int fields = in.nextInt();
            int numGreen = in.nextInt(); // SG
            int numBlue = in.nextInt(); // SB

            for (int i = 0; i < numGreen; i++) {
                green.add(in.nextInt());
            }
            for (int i = 0; i < numBlue; i++) {
                blue.add(in.nextInt());
            }
            while (!green.isEmpty() && !blue.isEmpty()) {
                int battles = 0;
                int diff = 0;
                while (!green.isEmpty() && !blue.isEmpty() && battles < fields) {
                    bf[battles][0] = green.peek();
                    green.poll();
                    bf[battles][1] = blue.peek();
                    blue.poll();
                    ++battles;
                }
                for (int i = 0; i < battles; ++i) {
                    diff = bf[i][0] - bf[i][1];
                    if (diff > 0)
                        green.add(diff);
                    else if (diff < 0)
                        blue.add(-1 * diff);
                }

            }

            if (!green.isEmpty()) {
                t.append("green wins" + "\n");
                while (!green.isEmpty()) {
                    t.append(green.peek());
                    t.append('\n');
                    green.poll();
                }
            } else if (!blue.isEmpty()) {
                t.append("blue wins" + "\n");
                while (!blue.isEmpty()) {
                    t.append(blue.peek());
                    t.append('\n');
                    blue.poll();
                }
            } else {
                t.append("green and blue died\n");
            }
            if (count > 1)
                t.append("\n");
        }
        System.out.print(t);
    }

    public static class reverseC implements Comparator<Integer> {

        @Override
        public int compare(Integer x, Integer y) {
            if (x > y)
                return -1;
            else if (x < y)
                return 1;
            else
                return 0;
        }

    }
}

有人知道这里发生了什么吗?

没关系。我想到了。我的计数语句:

if (count > 1) {
  t.append("\n");
}

应该是:

 if (count > 0) {
      t.append("\n");
    }

我修复了它,它非常有效。