国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ZCMU-Problem C - Sumsets

2019-11-11 06:31:50
字體:
來源:轉載
供稿:網友

PRoblem C: Problem C - Sumsets

Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 26  Solved: 18[Submit][Status][Web Board]

Description

Problem C - Sumsets

Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S.

Input

Several S, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of elements in S, followed by the elements of S, one per line. Each element of S is a distinct integer between -536870912 and +536870911 inclusive. The last line of input contains 0.

Output

For each S, a single line containing d, or a single line containing "no solution".

Sample Input

523571252166425610240

Sample Output

12no solution【解析】這道題的意思就是給我們一個集合,然后讓我們算集合當中有沒有存在a+b+c=d,如果有存在多個的要保證d是最大的那么其實我們就可以先給這個集合的元素先排序,然后再讓把a+b+c=d化成a+b=d-c,這樣我們可以通過二分查找來查找a和b的值,我們讓c取的是在a,b,c當中最大的值會比較好算...果然很多都可以用二分法來求..還是自己太菜了..
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int main(){    int n,i,j,a[1001],sum,left,right,m;    while(~scanf("%d",&n))    {        if(n==0)            break;        memset(a,0,sizeof(a));        m=-1;        for(i=0;i<n;i++)        {            scanf("%d",&a[i]);        }        sort(a,a+n);//排序整個集合中的元素        for(i=n-1;i>=0;i--)//d的取值        {            for(j=n-1;j>=0;j--)//c的取值            {                if(i==j)                    continue;                sum=a[i]-a[j];//a+b的值                left=0,right=j-1;//從0開始到j-1尋找a和b的值                while(left<right)                {                    if(a[left]+a[right]==sum)                    {                        m=a[i];                    goto loop;                    }                    else if(a[left]+a[right]>sum)                    {                        right--;                    }                    else                    {                        left++;                    }                }            }        }        loop:        if(m==-1)            printf("no solution/n");//沒有這樣的值        else            printf("%d/n",m);    }    return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 卢湾区| 十堰市| 资兴市| 珠海市| 同心县| 江安县| 乌兰浩特市| 长治县| 华池县| 长垣县| 阿拉善盟| 丹寨县| 秀山| 巩义市| 彰化市| 桓仁| 雅安市| 称多县| 绥宁县| 白银市| 应用必备| 静安区| 建湖县| 嘉义市| 湘乡市| 礼泉县| 福泉市| 望城县| 清徐县| 无棣县| 扬中市| 卫辉市| 游戏| 房产| 华阴市| 镇坪县| 华蓥市| 融水| 治多县| 古蔺县| 嵊泗县|