uva10125

题意:给出一个数集S中所有元素,问是否存在d属于S使得d=a+b+c,且a,b,c均属于S?若有,则输出最大的d 否则输出no solution

做法:暴力就可以过的。

代码如下:


#include <stdio.h>
#include <iostream>
#include <algorithm>
#define zz
using namespace std;
const int MAXN = 1000 + 5;
int s[MAXN];
int main(){
#ifndef zz
        freopen("in.txt","r",stdin);
#endif
        int n;
        while(scanf("%d", &n)!=EOF&&n){
                int i, j, k, l;
                for(i=0; i<n; i++)
                        scanf("%d", &s[i]);
                sort(s, s+n);
                bool ok=true;
                for(i=n-1; i&&ok; i--){
                        for(j=0;j<n&&ok; j++){
                                if(i==j) continue;
                                for(k=0; k<n&&ok; k++){
                                        if(i==k||j==k) continue;
                                        for(l=0; l<n&&ok; l++){
                                                if(i==l||j==l||k==l)continue;
                                                if(s[i]==s[j]+s[k]+s[l]){
                                                        printf("%d\n", s[i]);
                                                        ok=false;
                                                }
                                        }
                                }
                        }
                }
                if(ok)puts("no solution");
        }
        return 0;
}



原文地址:https://www.cnblogs.com/zjutzz/p/3207899.html