zoukankan      html  css  js  c++  java
  • USACO Section 3.2 Feed Ratios (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
    思路:居然没想到n^3方的暴力!!结果去解方程去了,那个0的情况整死我了!

    Executing...
       Test 1: TEST OK [0.000 secs, 3228 KB]
       Test 2: TEST OK [0.000 secs, 3228 KB]
       Test 3: TEST OK [0.000 secs, 3228 KB]
       Test 4: TEST OK [0.000 secs, 3228 KB]
       Test 5: TEST OK [0.000 secs, 3228 KB]
       Test 6: TEST OK [0.000 secs, 3228 KB]
    
    All tests OK.
      1 /*
      2 ID:wuhuaju2
      3 PROG:ratios
      4 LANG:C++
      5 */
      6 #include <cstdio>
      7 #include <iostream>
      8 #include <cstdlib>
      9 #include <algorithm>
     10 #include <cstring>
     11 #include <string>
     12 using namespace std;
     13 
     14 int tar[5],a[5],b[5],c[5];
     15 int d1,d2,d3,e1,e2,e3,kx,t1,t2,t3,t,x1,x2,x3;
     16 bool flag=false;
     17 
     18 void close()
     19 {
     20     fclose(stdin);
     21     fclose(stdout);
     22     exit(0);
     23 }
     24 
     25 void wrong()
     26 {
     27     cout<<"NONE"<<'\n';
     28     close();
     29 }
     30 
     31 void work()
     32 {
     33     d1=b[1]*a[2]-a[1]*b[2];
     34     d2=c[1]*a[2]-c[2]*a[1];
     35     e1=b[2]*a[3]-b[3]*a[2];
     36     e2=c[2]*a[3]-c[3]*a[2];
     37     d3=tar[1]*a[2]-tar[2]*a[1];
     38     e3=tar[2]*a[3]-tar[3]*a[2];
     39        t2=d2*e1-d1*e2;
     40         kx=d3*e1-e3*d1;
     41     //    printf("d1:%d d2:%d d3:%d e1:%d e2:%d e3:%d \n",d1,d2,d3,e1,e2,e3);
     42     //    printf("t2:%d kx:%d\n",t2,kx);
     43         if (d1==0 && d2==0 && e1==0 && e2==0)
     44         {
     45             cout<<"NONE"<<"\n";
     46             close();
     47         }
     48         if (t2==0)
     49         {
     50             for (int k=1;k<=100;k++)
     51             {
     52                 flag=false;
     53             for (int i=1;i<=3;i++)
     54             {
     55                 if (a[i]!=0)
     56                 {
     57                     if (tar[i]*k % a[i]!=0)
     58                     {
     59                         flag=true;
     60                         break;
     61                     }
     62                     x1=tar[i]*k / a[i];
     63                 }
     64                 if (b[i]!=0)
     65                 {
     66                     if (tar[i]*k % b[i]!=0)
     67                     {
     68                         flag=true;
     69                         break;
     70                     }
     71                     x2=tar[i]*k / b[i];
     72                 }
     73                 if (c[i]!=0)
     74                 {
     75                     if (tar[i]*k % c[i]!=0)    
     76                     {
     77                         flag=true;
     78                         break;
     79                     }
     80                     x3=tar[i]*k / c[i];
     81                 }
     82             }
     83         //    if (x1!=x2 || x2!=x3 || x1!=x3)
     84         //        break;
     85            if (not flag)
     86             {
     87             cout<<x1<<" "<<x2<<" "<<x3<<" "<<k<<endl;
     88             close();}
     89             }
     90         }
    //以下就是正常情况
    91 for (int k=1;k<=100;k++) 92 { 93 if (kx * k % t2==0) 94 { 95 x3=kx * k / t2; 96 if (x3>100 || x3<0) 97 { 98 cout<<"NONE"<<endl; 99 close(); 100 } 101 t=d3*k-d2*x3; 102 // printf("t:%d\n",t); 103 if (t % d1==0) 104 { 105 x2=t / d1; 106 if (x2>100 || x2<0) 107 continue; 108 t=tar[1]*k-c[1]*x3-b[1]*x2; 109 if (t % a[1]==0) 110 { 111 x1=t / a[1]; 112 if (x1>100 || x1<0) 113 continue; 114 cout<<x1<<" "<<x2<<" "<<x3<<" "<<k<<"\n"; 115 close(); 116 } 117 } 118 } 119 } 120 121 } 122 123 void init () 124 { 125 freopen("ratios.in","r",stdin); 126 freopen("ratios.out","w",stdout); 127 scanf("%d %d %d",&tar[1],&tar[2],&tar[3]); 128 scanf("%d %d %d",&a[1],&a[2],&a[3]); 129 scanf("%d %d %d",&b[1],&b[2],&b[3]); 130 scanf("%d %d %d",&c[1],&c[2],&c[3]); 131 } 132 133 int main () 134 { 135 init(); 136 work(); 137 close(); 138 return 0; 139 }
  • 相关阅读:
    Jser 设计模式系列之面向对象
    jQuery 2.0.3 源码分析 回溯魔法 end()和pushStack()
    jQuery 2.0.3 源码分析 数据缓存
    jQuery 2.0.3 源码分析 Deferred(最细的实现剖析,带图)
    jQuery 2.0.3 源码分析 Deferred概念
    jQuery 2.0.3 源码分析 回调对象
    试试看 ? 离奇古怪的javascript题目
    jQuery 2.0.3 源码分析Sizzle引擎
    设计模式之美:Memento(备忘录)
    设计模式之美:Mediator(中介者)
  • 原文地址:https://www.cnblogs.com/cssystem/p/2910716.html
Copyright © 2011-2022 走看看