HUT-1551 硬币与桌子

1551: E

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 28  Solved: 4
[Submit][Status][Web Board]

Description

The Royal Canadian Mint has commissioned a new series of designer coffee tables, with legs that are constructed from stacks of coins. Each table has four legs, each of which uses a different type of coin. For example, one leg might be a stack of quarters, another nickels, another loonies, and another twonies. Each leg must be exactly the same length.
Many coins are available for these tables, including foreign and special commemorative coins. Given an inventory of available coins and a desired table height, compute the lengths nearest to the desired height for which four legs of equal length may be constructed using a different coin for each leg.

Input

Input consists of several test cases. Each case begins with two integers: 4 <= n <= 50 giving the number of types of coins available, and 1 <= t <= 10 giving the number of tables to be designed. n lines follow; each gives the thickness of a coin in hundredths of millimetres. t lines follow; each gives the height of a table to be designed (also in hundredths of millimetres). A line containing 0 0 follows the last test case.

Output

For each table, output a line with two integers: the greatest leg length not exceeding the desired length, and the smallest leg length not less than the desired length.

Sample Input

4 2
50
100
200
400
1000
2000
0 0

Sample Output

800 1200
2000 2000
  
 
  坑爹啊,暴力过。求出所有四个数组合中的最小公倍数,在通过浮点运算的取上下整得到最佳值。
  代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h> 
#include <math.h>

struct E
{
    int val, cil, flr;
}e[55];

int q[15], N, Q;

int gcd( int a, int b )
{
    if( b== 0 )
    {
        return a;
    }
    else
    {
        return gcd( b, a% b );
    }
}

int lcm( int a, int b )
{
    return a* b/ gcd( a, b );
}

void get( int &cil, int &flr, int q )
{
    for( int i= 1; i<= N- 3; ++i )
    {
        for( int j= i+ 1; j<= N- 2; ++j )
        {
            int L1= lcm( e[i].val, e[j].val );
            for( int k= j+ 1; k<= N- 1; ++k )
            {
                int L2= lcm( e[k].val, L1 );
                for( int m= k+ 1; m<= N; ++m )
                {
                    int L3= lcm( L2, e[m].val );
                    int x= L3* ( int )floor( 1.0* q/ L3 );
                    int y= L3* ( int )ceil( 1.0* q/ L3 );
                    if( x> flr )
                    {
                        flr= x;
                    }
                    if( y< cil )
                    {
                        cil= y;
                    }
                }
            }
        }
    }
}


int main(  )
{
    while( scanf( "%d %d", &N, &Q ), N| Q )
    {
        for( int i= 1; i<= N; ++i )
        {
            scanf( "%d", &e[i].val );
        }
        for( int i= 1; i<= Q; ++i )
        {
            int cil= 0x7fffffff, flr= -1;
            scanf( "%d", &q[i] );
            get( cil, flr, q[i] );
            printf( "%d %d\n", flr, cil );
        }
    }
    return 0;
}
posted @ 2011-07-30 19:53  沐阳  阅读(300)  评论(0编辑  收藏  举报