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

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

ZCMU-Problem C - Sumsets

2019-11-11 05:38:56
字體:
供稿:網(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【解析】這道題的意思就是給我們一個(gè)集合,然后讓我們算集合當(dāng)中有沒有存在a+b+c=d,如果有存在多個(gè)的要保證d是最大的那么其實(shí)我們就可以先給這個(gè)集合的元素先排序,然后再讓把a(bǔ)+b+c=d化成a+b=d-c,這樣我們可以通過二分查找來查找a和b的值,我們讓c取的是在a,b,c當(dāng)中最大的值會(huì)比較好算...果然很多都可以用二分法來求..還是自己太菜了..
#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);//排序整個(gè)集合中的元素        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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 岢岚县| 黄龙县| 门源| 兴化市| 高要市| 开远市| 永修县| 内黄县| 呼图壁县| 桑植县| 峨边| 高邮市| 广灵县| 临海市| 蒙城县| 大余县| 鄱阳县| 通许县| 金坛市| 泰顺县| 杭锦后旗| 介休市| 理塘县| 诸城市| 湟中县| 郓城县| 潼关县| 济阳县| 蕉岭县| 从化市| 淳化县| 尤溪县| 通河县| 乌鲁木齐市| 太保市| 彭水| 汝南县| 武隆县| 宁国市| 台江县| 陵川县|