题目描述:ZOJ ACM 1610
题目并不难,但是我在提交时总是提示runtime error.
原因应该是因为我对数组的处理不够严谨,出现了-1的情况,所以才会报错的吧。
public class Main {
public static void main(String argv[]) {
java.util.Scanner scanner = new java.util.Scanner(System.in);
while(scanner.hasNext()) {
int lines = Integer.parseInt(scanner.nextLine());
int line[] = new int[8001];
int colors[] = new int[8001];
for(int i=0;i<8001;i++) {
line[i] = -1;
colors[i] = 0;
}
int maxEnd = 0;
for(int i=0;i<lines;i++) {
String strLine = scanner.nextLine();
String strN[] = strLine.split(" ");
int start = Integer.parseInt(strN[0]);
int end = Integer.parseInt(strN[1]);
int color = Integer.parseInt(strN[2]);
maxEnd = Math.max(end, maxEnd);
for(int j=start;j<end;j++) {
line[j] = color;
}
}
int currentcolor = -1;
for(int k=0;k<maxEnd;k++) {
if(line[k] != currentcolor) {
currentcolor = line[k];
if(currentcolor!=-1) //这个判断很重要!因为题目给出的线段不一定是连续的,如果不连续,颜色为设置,currentcolor就为-1,那么colors[currentcolor]就会出现colors[-1]的情况,所以提交后才会出现runtime error错误!
colors[currentcolor] += 1;
}
}
//print
for(int l=0;l<8001;l++) {
if(colors[l] >0) {
System.out.println(l+" "+colors[l]);
}
}
System.out.println("");
}
}
}