19_09_02(小明种苹果(续))

小明种苹果(续)

第一次(40)

最后证明:和输入超时无关,和long类型无关.

package csp;

//long 类型
//scanner 读取超时?
//4
//4 74 -7 -12 -5
//5 73 -8 -6 59 -4
//5 76 -5 -10 60 -2
//5 80 -6 -15 59 0
import java.util.Scanner;
public class test {
public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	int n = in.nextInt();
	long[][] a = new long[n][3];
	int T=0,D=0,E=0;
	for(int i =0;i<n;i++) {
		int m = in.nextInt();
		a[i][0] = in.nextLong();
		boolean flag = true;
		for(int j =1;j<m;j++) {
			//long temp = in.nextInt();
			long temp = in.nextLong();
			if(flag) {
				if(temp<=0) a[i][0] += temp;
				if(temp>0) {
					a[i][1] = temp;//记录同一棵树统计第二次时的果数
					flag = false;
				}
			}
			if(!flag) {
				a[i][2] += temp;//累加同一棵树统计第二次时的果数 
			}
		}
		
		if(a[i][1] == 0 && a[i][0] !=0 ) {
			a[i][1] = a[i][0];//补齐 方便后值计算
			a[i][2] = a[i][0];
		}
		
	}//for
	in.close();
	for(int i =0;i<n;i++) {
		T+=a[i][2];
		if(a[i][0] - a[i][1] > 0) {D++; //落果棵树
		//System.out.println(i+" "+a[i][0]+" "+a[i][1]);
		}
		//统计组数
		if(i==0) {
			if((a[i][0]-a[i][1] > 0) && (a[n-1][0]-a[n-1][1] > 0) && (a[i+1][0]-a[i+1][1] > 0)) {
				E ++;
			}
		}
		if(i == n-1) {
			if((a[i][0]-a[i][1] > 0) && (a[i-1][0]-a[i-1][1] > 0) && (a[0][0]-a[0][1] > 0)) {
				E ++;
			}
		}
		if(i >0 && i< n-1) {
			if((a[i][0]-a[i][1] > 0) && (a[i-1][0]-a[i-1][1] > 0) && (a[i+1][0]-a[i+1][1] > 0)) {
				E ++;
			}
		}
		
	}
	
	System.out.print(T+" "+D+" "+E);
}
}

//5
//4 10 0 9 0
//4 10 -2 7 0
//2 10 0
//4 10 -3 5 0
//4 20 -1 8 0

第二次(100)



//4
//4 74 -7 -12 -5
//5 73 -8 -6 59 -4
//5 76 -5 -10 60 -2
//5 80 -6 -15 59 0
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	int n = in.nextInt();
	in.nextLine();//in.nextInt()之后再读一下 以进入到输入n的下一行,否则会出错
	int[] a = new int[n+2];//直接将前两个值放到后面 减少了分情况讨论
	int T=0,D=0,E=0;
	for(int i =0;i<n;i++) {
		String line = in.nextLine();
		String[]  s = line.trim().split(" ");	//	去除多余空格、再以空格分割数字
		int  m = Integer.parseInt(s[0]);// String 转int
		int currentcount = Integer.parseInt(s[1]);// String 转int
		boolean flag = false;
		for(int j =2;j<=m;j++) {

			int temp = Integer.parseInt(s[j]);
			
			if(temp > 0) {//重新计数
				if(currentcount > temp && !flag) {//看是否落果 
					flag = true;
					a[i]  = 1;//记录当前位置表示落果了
					D++;//记录棵树
				}	
				currentcount = temp; //不论是否落果 该值都要更新
				
			}else {//落果累加
				currentcount += temp;
			}
		}
		T += currentcount ;//总数目累加
	}//for
	in.close();
	
	a[n] = a[0];//绕后
	a[n+1] = a[1];
	for(int i =1;i<n+1;i++) {
		if(a[i] == 1 && a[i-1] == 1 && a[i+1] == 1) {//相邻三棵树发生落果
			E++;
		}
	}		
	System.out.print(T+" "+D+" "+E);
}
}

//5
//4 10 0 9 0
//4 10 -2 7 0
//2 10 0
//4 10 -3 5 0
//4 20 -1 8 0


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值