2013年2月10日 星期日

ACM 10125 Sumsets


#include <stdio.h>

void bubble_sort(int *arr, int n)
{
    int i, j, temp;

    for (i = n-1; i >= 1; --i)
        for (j = 0; j < i; ++j)
            if (arr[j] > arr[j+1]) {
                temp = arr[j+1];
                arr[j+1] = arr[j];
                arr[j] = temp;
            }
}

int main(void)
{
    int n, i, a, b, c, d, found, set[1000], sum;

    while (scanf("%d", &n) == 1 && n) {
        for (i = 0; i < n; ++i)
            scanf("%d", &set[i]);

        bubble_sort(set, n);

        found = 0;
        for (d = n-1; !found && d >= 0; --d)
            for (a = 0; !found && a < n; ++a)
                for (b = a+1; !found && b < n; ++b)
                    for (c = b+1; !found && c < n; ++c) {
                        sum = set[a] + set[b] + set[c];
                        if (sum == set[d]
                            && set[a] != sum && set[b] != sum && set[c] != sum)
                            found = 1;
                    }

        if (found)
            printf("%d\n", sum);
        else
            printf("no solution\n");
    }

    return 0;
}

沒有留言:

張貼留言