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;
}

转载于:https://www.cnblogs.com/Lyush/archive/2011/07/30/2122146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值