sicily6135. Blackjack

来源:互联网 发布:直播淘宝小二 编辑:程序博客网 时间:2024/06/05 20:18

鉴于这道题目的数据量并不大,所以,暴力解决就可以啦。。

所谓暴力,也就说,三重循环。。

题目的链接: http://soj.me/6135

#include <iostream>#include <stdio.h>using namespace std;int main(){    int n, m;    int count = 1;    while (scanf("%d%d", &n, &m) != EOF)    {        int max = -1;        int a[109];        for (int i = 0; i < n; ++ i)        {            scanf("%d", &a[i]);        }        int temp = 0;        for (int i = 0; i < n; ++ i)        {            for (int j = i + 1; j < n; ++ j)            {                for (int k = j + 1; k < n; ++ k)                {                    temp = a[i] + a[j] + a[k];                    if (temp < m && temp > max)                    {                        max = temp;                        temp = 0;                    }                    else if (temp == m)                    {                        max = m;                        i = n;                        j = n;                        break;                    }                    else if (temp > m)                    {                        continue;                    }                }            }        }        //printf("样式%d:\n", count);        printf("%d\n", max);        ++ count;    }}