ZOJ ACM 1610(JAVA)

题目描述: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("");
		}
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值