2013年9月28日 星期六

UVA 11565 - Simple Equations

Problem link

Keyword: brute force

Algorithm:

The upper bound for these 3 integers is sqrt( C ).
Scan through x, y, z from -sqrt(C) to sqrt(C), and check the validity.
Remember x, y, z are distinct.

Code:
#include<cstdio>
#include<cmath>

int main()
{
    int t, sum, pro, sq;
    scanf( "%d", &t );

    for( int i = 0; i < t; ++i )
    {
        scanf( "%d %d %d", &sum, &pro, &sq );
        int lim = sqrt( sq );
        bool found = false;
        for( int a = -lim; a <= lim && !found; ++a )
            for( int b = -lim; b <= lim && !found; ++b )
                for( int c = -lim; c <= lim && !found; ++c )
                    if( a != b && b != c && a != c && a+b+c == sum && a*b*c == pro && a*a + b*b + c*c == sq )
                        printf( "%d %d %d\n", a, b, c ), found = true;
        if( !found )
            puts( "No solution." );
    }

    return 0;
}

沒有留言:

張貼留言