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;
	}
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章