算出number(一行保存一个数字)文件中和最大的连续行数,打印开始结束行号,打印最大的和



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;


public class JFQ_Number {
private static final String FILE_PATH = "/home/lifeix/number";
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
startNumber();
}


private static void startNumber() throws IOException {
FileInputStream fis = new FileInputStream(new File(FILE_PATH));
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = br.readLine();
int count = 0;
int currentSum = 0;
int startLine = 0;
int endLine = 0;
int lastSum = currentSum;
int tempSum = 0;

while(line != null) {
count++;
int l = Integer.parseInt(line.trim());
if(count <3) {
currentSum += l;
endLine = count;
lastSum = currentSum;
} else {
tempSum = currentSum;
currentSum += l;

if(tempSum < currentSum) {
if(currentSum < l) {
currentSum = l;
startLine = count;
}
if(lastSum < currentSum) {
lastSum = currentSum;
endLine = count;
}
} else {
if(lastSum < tempSum) {
lastSum = tempSum;
}
}

}
line = br.readLine();
}
System.out.println("startLine=" + startLine + ", endLine=" +endLine + ", sumline=" + lastSum);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值