HUT-XXXX Sum of number 暴力

这题数据范围小,就直接暴力了,没什么好讨论的了。

代码如下:

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

int N, M, seq[150];

int main()
{
    int ret;
    while (scanf("%d %d", &N, &M) == 2) {
        ret = 0;
        for (int i = 1; i <= N; ++i) {
            scanf("%d", &seq[i]);
        }
        for (int i = 1; i <= N-2; ++i) {
            for (int j = i+1; j <= N-1; ++j) {
                for (int k = j+1; k <= N; ++k) {
                    if (seq[i] + seq[j] + seq[k] <= M) {
                        ret = max(ret, seq[i] + seq[j] + seq[k]);
                    }
                }
            }
        }
        printf("%d\n", ret);
    }
    return 0;
}
posted @ 2012-07-24 00:08  沐阳  阅读(743)  评论(0编辑  收藏  举报