USACO--3.2Feed Ratios

來源:互聯網
上載者:User

標籤:usaco

WF上的水題,直接類比即可.注意為0情況的處理,我寫的有點複雜了.

代碼如下:

/*ID: 15674811LANG: C++TASK: ratios*/#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define INF 0x3f3f3f3fint xx[5],yy[5],zz[5],k4,sum,tmp;int check(int x,int y,int z){       if(!xx[1]&&x)  return 0;       if(!yy[1]&&y)  return 0;       if(!zz[1]&&z)   return 0;       if(xx[1]==0)       {            if(yy[1]==0)            {                 tmp=z/zz[1];                 return 1;            }            if(zz[1]==0)            {                tmp=y/yy[1];                return 1;            }            if(y%yy[1]||z%zz[1])                 return 0;            if(y/yy[1]==z/zz[1])            {                  tmp=y/yy[1]; return 1;            }            return 0;       }       if(yy[1]==0)       {           if(zz[1]==0)           {                tmp=x/xx[1];                return 1;           }           if(x%xx[1]||z%zz[1])                return 0;           if(x/xx[1]==z/zz[1])           {                  tmp=x/xx[1];                  return 1;            }            return 0;       }       if(zz[1]==0)       {            if(x%xx[1]||y%yy[1])                return 0;            if(x/xx[1]==y/yy[1])             {                   tmp=y/yy[1];  return 1;            }            return 0;       }       if(x%xx[1]||y%yy[1]||z%zz[1])           return 0;       if(x/xx[1]==y/yy[1]&&x/xx[1]==z/zz[1])       {            tmp=x/xx[1];            return 1;       }      return 0;}int main(){      freopen("ratios.in","r",stdin);      freopen("ratios.out","w",stdout);       //freopen("in.txt","r",stdin);       for(int i=1;i<=4;i++)                 scanf("%d%d%d",&xx[i],&yy[i],&zz[i]);      sum=INF;      int kk1,kk2,kk3;      k4=-1;      for(int k1=0;k1<100;k1++)          for(int k2=0;k2<100;k2++)              for(int k3=0;k3<100;k3++)              {                    int x=k1*xx[2]+k2*xx[3]+k3*xx[4];                    int y=k1*yy[2]+k2*yy[3]+k3*yy[4];                    int z=k1*zz[2]+k2*zz[3]+k3*zz[4];                    if(x==0&&y==0&&z==0)                         continue;                    if(check(x,y,z)&&sum>(k1+k2+k3))                    {                           kk1=k1,kk2=k2,kk3=k3;                           k4=tmp;                           sum=k1+k2+k3;                    }              }        if(k4!=-1)              printf("%d %d %d %d\n",kk1,kk2, kk3,k4);        else              printf("NONE\n");    return 0;}

USACO--3.2Feed Ratios

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.