# 3.2.4 Feed Ratios 饲料调配

3.2.4 Feed Ratios 饲料调配

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

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

Line 1: 三个用空格分开的整数，表示目标饲料
Line 2..4: 每行包括三个用空格分开的整数，表示农夫约翰买进的饲料的比例

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

``8 1 5 7 ``

``````#include <iostream>
using namespace std;

typedef struct
{
int x;
int y;
int z;
}ratio;

ratio dr;
ratio rr[3];
bool flag;

int main()
{
int i,j,k;
int tmpx,tmpy,tmpz,res;
cin>>dr.x>>dr.y>>dr.z;
for(i=0;i<3;i++)
cin>>rr[i].x>>rr[i].y>>rr[i].z;
flag=false;
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
for(k=0;k<100;k++)
{
tmpx=rr[0].x*i+rr[1].x*j+rr[2].x*k;
tmpy=rr[0].y*i+rr[1].y*j+rr[2].y*k;
tmpz=rr[0].z*i+rr[1].z*j+rr[2].z*k;
if(((dr.x!=0)&&(tmpx==0))||((dr.y!=0)&&(tmpy==0))||((dr.z!=0)&&(tmpz==0)))
continue;
if(((tmpx!=0)&&(tmpx%dr.x!=0))||((tmpy!=0)&&(tmpy%dr.y!=0))||((tmpz!=0)&&(tmpz%dr.z!=0)))
continue;
if((tmpx*dr.y==tmpy*dr.x)&&(tmpy*dr.z==tmpz*dr.y))
{
if(tmpx!=0)
res=tmpx/dr.x;
else if(tmpy!=0)
res=tmpy/dr.y;
else if(tmpz!=0)
res=tmpz/dr.z;
flag=true;
break;
}
}
if(flag==true)
break;
}
if(flag==true)
break;
}
if(flag==false)
cout<<"NONE"<<endl;
else
cout<<i<<" "<<j<<" "<<k<<" "<<res<<endl;;
return 0;
}
``````

``````#include <iostream>
using namespace std;

typedef struct
{
int x;
int y;
int z;
}ratio;

ratio dr;
ratio rr[3];
bool flag;

int main()
{
int i,j,k;
int tmpx,tmpy,tmpz,res;
cin>>dr.x>>dr.y>>dr.z;
for(i=0;i<3;i++)
cin>>rr[i].x>>rr[i].y>>rr[i].z;
flag=false;
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
for(k=0;k<100;k++)
{
tmpx=rr[0].x*i+rr[1].x*j+rr[2].x*k;
tmpy=rr[0].y*i+rr[1].y*j+rr[2].y*k;
tmpz=rr[0].z*i+rr[1].z*j+rr[2].z*k;
if(((dr.x!=0)&&(tmpx==0))||((dr.y!=0)&&(tmpy==0))||((dr.z!=0)&&(tmpz==0)))
continue;
if(((tmpx!=0)&&(tmpx%dr.x!=0))||((tmpy!=0)&&(tmpy%dr.y!=0))||((tmpz!=0)&&(tmpz%dr.z!=0)))
continue;
if((tmpx*dr.y==tmpy*dr.x)&&(tmpy*dr.z==tmpz*dr.y))
{
if(tmpx!=0)
res=tmpx/dr.x;
else if(tmpy!=0)
res=tmpy/dr.y;
else if(tmpz!=0)
res=tmpz/dr.z;
flag=true;
break;
}
}
if(flag==true)
break;
}
if(flag==true)
break;
}
if(flag==false)
cout<<"NONE"<<endl;
else
cout<<i<<" "<<j<<" "<<k<<" "<<res<<endl;;
return 0;
}
``````