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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

ZCMU-Problem C - Sumsets

2019-11-11 05:04:51
字體:
供稿:網(wǎng)友

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【解析】這道題的意思就是給我們一個集合,然后讓我們算集合當(dāng)中有沒有存在a+b+c=d,如果有存在多個的要保證d是最大的那么其實我們就可以先給這個集合的元素先排序,然后再讓把a+b+c=d化成a+b=d-c,這樣我們可以通過二分查找來查找a和b的值,我們讓c取的是在a,b,c當(dāng)中最大的值會比較好算...果然很多都可以用二分法來求..還是自己太菜了..
#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(luò)-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;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永德县| 克拉玛依市| 龙山县| 乐安县| 福贡县| 翼城县| 福海县| 杭锦旗| 孟津县| 屯留县| 宜都市| 鄄城县| 绥宁县| 上栗县| 怀仁县| 如东县| 承德县| 沈丘县| 鹤峰县| 邢台县| 昭平县| 二手房| 上林县| 耿马| 启东市| 麻江县| 揭东县| 奇台县| 普兰店市| 保康县| 濉溪县| 洪湖市| 辽中县| 怀化市| 界首市| 武定县| 凉城县| 安远县| 鹤峰县| 九江市| 黑龙江省|