ZOJ 1101

View Code
 1 #include<stdio.h>//二分搜索技术运用。此题在排序后应用减法从右往左。
 2 #include<stdlib.h>
 3 #include<string.h>
 4 
 5 int a[1001],i,j,k,ncases;
 6 int search(int num,int from,int end)
 7 {
 8     int mid;
 9     while(from <= end) 
10     { 
11        mid = (from+end)/2; 
12        if(num == a[mid])
13             return 1;
14        if(num < a[mid])
15           end = mid-1;              
16        else
17            from = mid+1;
18     }
19     return 0;
20 }
21 
22 int cmp(const void *a,const void *b)
23 {
24     return *(int *)a - *(int *)b;
25 }
26              
27 int main()
28 {     
29    int sum,tmp,flag;   
30    
31    while(scanf("%d",&ncases)&&ncases)
32    { 
33       for(i=0; i<ncases; i++)
34       {
35         scanf("%d",&a[i]);
36       } 
37       qsort(a,ncases,sizeof(a[0]),cmp);        
38       flag = 0;
39       for(i=ncases-1; i>=0; i--)
40       {
41         for(j=ncases-1; j>=0; j--)    
42         { 
43            if(i == j) continue;      
44            tmp = a[i] - a[j];        
45            for(k=ncases-1; k>=0; k--)
46            { 
47              if(i==k || j==k)
48                 continue;       
49              sum = tmp - a[k]; 
50              if(sum==a[i] || sum==a[j] || sum==a[k])//防止重复
51                  continue;
52              if(search(sum,0,ncases-1)) 
53              {
54                 printf("%d\n",a[i]);     
55                 flag = 1; 
56                 goto H;
57              }                           
58            }
59           }
60         } 
61        if( !flag )   
62           printf("no solution\n");
63      H: if(flag==1)  continue;               
64    }
65      
66    return 0;
67 }   
posted @ 2012-08-01 23:15  zhongya  阅读(175)  评论(0编辑  收藏  举报