• USACO3.2.4Feed Ratios


    Feed Ratios
    1998 ACM Finals, Dan Adkins

    Farmer John feeds his cows only the finest mixture of cow food, which has three components: Barley, Oats, and Wheat. While he knows the precise mixture of these easily mixable grains, he can not buy that mixture! He buys three other mixtures of the three grains and then combines them to form the perfect mixture.

    Given a set of integer ratios barley:oats:wheat, find a way to combine them IN INTEGER MULTIPLES to form a mix with some goal ratio x:y:z.

    For example, given the goal 3:4:5 and the ratios of three mixtures:

            1:2:3
            3:7:1
            2:1:2
    

    your program should find some minimum number of integer units (the `mixture') of the first, second, and third mixture that should be mixed together to achieve the goal ratio or print `NONE'. `Minimum number' means the sum of the three non-negative mixture integers is minimized.

    For this example, you can combine eight units of mixture 1, one unit of mixture 2, and five units of mixture 3 to get seven units of the goal ratio:

        8*(1:2:3) + 1*(3:7:1) + 5*(2:1:2) = (21:28:35) = 7*(3:4:5)
    

    Integers in the goal ratio and mixture ratios are all non-negative and smaller than 100 in magnitude. The number of units of each type of feed in the mixture must be less than 100. The mixture ratios are not linear combinations of each other.

    PROGRAM NAME: ratios

    INPUT FORMAT

    Line 1: Three space separated integers that represent the goal ratios
    Line 2..4: Each contain three space separated integers that represent the ratios of the three mixtures purchased.

    SAMPLE INPUT (file ratios.in)

    3 4 5
    1 2 3
    3 7 1
    2 1 2
    

    OUTPUT FORMAT

    The output file should contain one line containing four integers or the word `NONE'. The first three integers should represent the number of units of each mixture to use to obtain the goal ratio. The fourth number should be the multiple of the goal ratio obtained by mixing the initial feed using the first three integers as mixing ratios.

    SAMPLE OUTPUT (file ratios.out)

    8 1 5 7
    

     题解:看到题目中的系数都会小于100,即枚举量等于10^3。果断枚举了。要注意被零除的情况。效率还不错,最慢的0.011s。据说还有解方程的。。全部0s秒杀,不过我不会。。。

    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:ratios
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 #include<string.h>
     8 struct tatio
     9 {
    10     int x,y,z;
    11 };
    12 int main(void)
    13 {
    14     freopen("ratios.in","r",stdin);
    15     freopen("ratios.out","w",stdout);
    16     int x,y,z,i,j,k,t,a,b,c;
    17     long a1,b1,c1;
    18     struct tatio rt[3];
    19     scanf("%d%d%d",&x,&y,&z);
    20     for(i=0; i<3; i++)
    21         scanf("%d%d%d",&rt[i].x,&rt[i].y,&rt[i].z);
    22     for(i=0; i<100; i++)
    23         for(j=0; j<100; j++)
    24             for(k=0; k<100; k++)
    25                 if(i!=0||j!=0||k!=0)
    26                 {
    27 
    28                     a1=i*rt[0].x+j*rt[1].x+k*rt[2].x;
    29                     b1=i*rt[0].y+j*rt[1].y+k*rt[2].y;
    30                     c1=i*rt[0].z+j*rt[1].z+k*rt[2].z;
    31                     if(a1!=0&&x==0) continue;
    32                     if(b1!=0&&y==0) continue;
    33                     if(c1!=0&&z==0) continue;
    34                     t=a1/x;
    35                     if(a1%x==0)
    36                     {
    37                         a=t*x;
    38                         b=t*y;
    39                         c=t*z;
    40                         if(a==a1&&b==b1&&c==c1)
    41                         {
    42                             printf("%d %d %d %d\n",i,j,k,t);
    43                             return 0;
    44                         }
    45                     }
    46 
    47                 }
    48     printf("NONE\n");
    49     return 0;
    50 }
  • 相关阅读:
    Python批量爬取唯美类图片
    查找Windows启动程序
    在Windows10中进行蓝屏排查
    在Windows 10中应该记住的完整的多指触摸板手势
    通过命令行工具远程注销用户
    Windows 7,8.1和10中的隐藏退出资源管理器选项
    找出正在使用的全局热键
    windows10中创建环境变量
    mysql高级(锁机制)
    mysql高级(查询截取分析)
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2933206.html
Copyright © 2020-2023  润新知