UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)

Maximum Sub-sequence Product 

Bob needs money, and since he knows you are here, he decided to gambleintelligently. The game is rather simple: each player gets a sequence ofintegers. The players must determine, by using their mega-pocket computers,which is the maximum product value which can be computed with non emptysub-sequences of consecutive numbers from the given sequence. The winner isthe one which gets first the right result.


Can you help Bob with a program to compute quickly the wanted product,particularly when the sequence is quite long ?

Input 

The input file contains sequences of numbers. Eachnumber will have at most 5 digits. There will be atmost 100 numbers in each sequence. Each sequence starts on a newline and may continue on several subsequent lines. Each sequence ends withthe number -999999 (which is not part of the sequence).

Output 

The maximum sub-sequence product for each sequence must be written on thestandard output, on a different line. A simple example is illustrated inthe sample below.

Sample Input 

1 2 3 -999999
-5 -2 2 -30 -999999
-8 -999999
-1 0 -2 -999999

Sample Output 

6
120
-8
0


import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.math.BigInteger;

public class Main
{
	public static final boolean DEBUG = false;
	
	public StreamTokenizer tokenizer;
	public BufferedReader cin;
	public PrintWriter cout;
	public ArrayList<Integer> v = new ArrayList<Integer>();
	
	public void init()
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String next()
	{
		try {
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF)
				return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
				return String.valueOf((int)tokenizer.nval);
			} else if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
				return tokenizer.sval;
			} else return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean input()
	{
		v.clear();
		while (true) {
			String s = next();
			if (s == null) return false;
			if ("-999999".compareTo(s) == 0) break;
			else {
				v.add(Integer.parseInt(s));
			}
		}
		
		return true;
	}
	
	public void solve()
	{
		BigInteger Min = BigInteger.valueOf(v.get(0)), Max = Min;
		BigInteger ans = Max;
		
		for (int i = 1, len = v.size(); i < len; i++) {
			BigInteger M = BigInteger.valueOf(v.get(i));
			if (M.compareTo(BigInteger.valueOf(v.get(i)).multiply(Max)) < 0) {
				M = BigInteger.valueOf(v.get(i)).multiply(Max);
			}
			
			if (M.compareTo(BigInteger.valueOf(v.get(i)).multiply(Min)) < 0) {
				M = BigInteger.valueOf(v.get(i)).multiply(Min);
			}
			
			BigInteger m = BigInteger.valueOf(v.get(i));
			if (m.compareTo(BigInteger.valueOf(v.get(i)).multiply(Max)) > 0) {
				m = BigInteger.valueOf(v.get(i)).multiply(Max);
			}
			
			if (m.compareTo(BigInteger.valueOf(v.get(i)).multiply(Min)) > 0) {
				m = BigInteger.valueOf(v.get(i)).multiply(Min);
			}
			
			Max = M;
			Min = m;
			
			if (ans.compareTo(Max) < 0) {
				ans = Max;
			}
		}
		
		cout.println(ans.toString());
		cout.flush();
	}
	
	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			solver.solve();
		}
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值