chapter05-Gamblers(ZOJ 1101)

Gamblers

A group of n gamblers decide to play a game:

At the beginning of the game each of them will coverup his wager on the table and the assitant must make sure that there are no twogamblers have put the same amount. If one has no money left, one may borrowsome chips and his wager amount is considered to be negative. Assumethat they all bet integer amount of money.

Then when they unveil their wagers, the winner is theone who's bet is exactly the same as the sum of that of 3 other gamblers. Ifthere are more than one winners, the one with the largest bet wins.

For example, suppose Tom, Bill, John, Roger and Bushbet $2, $3, $5, $7 and $12, respectively. Then the winner is Bush with $12since $2 + $3 + $7 = $12 and it's the largest bet.

Input

Wagers of several groups of gamblers, each consistingof a line containing an integer 1 <= n <= 1000 indicating the number ofgamblers in a group, followed by their amount of wagers, one per line. Eachwager is a distinct integer between -536870912 and +536870911 inclusive. Thelast line of input contains 0.

Output

For each group, a single line containing the wageramount of the winner, or a single line containing "no solution".

Sample Input

5

2

3

5

7

12

5

2

16

64

256

1024

0

Output for Sample Input

12

no solution

 

题目的大致意思是:

    就是输入一串n无序的数,数的范围在-536870912 和 +536870911之前,(三个数相加最大也不会大于两个字节),n的范围在1 <= n <= 1000之间,从这n个数中,任意找出三个数,同时使这三个数相加的值能够在这串数中找到,如果存在多余一种情况那么取最大值;

输入格式:

第一行:数的个数

数1,数2.。。。。

输入0表结束;

 

 

分析:

         如果用暴力搜索方法的话,我们任意取三个数,(遍历需要n^3时间)得到的结果在这串数中查找是否存在(用二分搜索需要logn,前提是先排好序);

         因为这个数可能不只一个,所以我们可以先排好序,从后往前遍历,那么能够保证第一次获得的数是最大的;

(时间复杂度为:n^3logn)

这道题,看起来很简单,但是事实上,有许多需要注意的地方;

1:怎么样才能做到保证第一次获得的数最大,需要做减法才能实现(详细请看代码,做加法还真没办法)

2:因为负数的存在,第一个数也可以是需要求得的值;

比如-6 -1 -2 – 3 这样的一组数能得到好的结果

3:注意同个数不能被操作两次,同时每个数都不一样(要知道它的用处,如果这个条件不存在,那么又是相当的麻烦)

ZOJ的显示结果如下:


//写的代码不是很简洁,方法也是通用的方法;
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;

public class Main { 
	private static int[] dataArray;
	private static int finalAns;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n;
		
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		while((n=cin.nextInt())!=0){
			dataArray = new int[n];
			for(int i=0;i<n;i++){
				dataArray[i] = cin.nextInt();
			}
			
			if(getTheNum() == 0){
				System.out.println("no solution");
			}else{
				System.out.println(finalAns);
			}
		}
		
	}

	private static int getTheNum() {
		// TODO Auto-generated method stub
		//排序
		int temp;
		int index;
		Arrays.sort(dataArray);

		for(int i = dataArray.length -1;i>=0;i--){
			for(int j=0;j<dataArray.length-1;j++){
				if(j ==i){
					continue;
				}
				for(int k=j+1;k<dataArray.length;k++){
					if(k == i){
						continue;
					}
					temp = dataArray[i]-dataArray[j]-dataArray[k];
					if((index =Arrays.binarySearch(dataArray,temp))>=0){
						if(index != k && index !=j && index !=i){
							finalAns = dataArray[i];
							return 1;
						}
					}
				}
			}
		}
		return 0;
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值