这题就是经典的配饲料, 由于最多配比是100因此可以用暴力解决法, 注意下mod 0的处理, 代码如下:
/* ID: m1500293 LANG: C++ PROG: ratios */ #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int si[10]; int a[10][10]; int res[10], sum; void deal(int ii, int jj, int kk) { int now[5]; now[0] = ii*a[0][0] + jj*a[1][0] + kk*a[2][0]; now[1] = ii*a[0][1] + jj*a[1][1] + kk*a[2][1]; now[2] = ii*a[0][2] + jj*a[1][2] + kk*a[2][2]; for(int i=0; i<3; i++) if(si[i]==0 && now[i]!=0) return ; int div[5], len=0; for(int i=0; i<3; i++) { if(si[i]==0) continue; if(now[i]%si[i]!=0) return; div[len++] = now[i]/si[i]; } for(int i=1; i<len; i++) if(div[i]!=div[i-1]) return; int sm = ii+jj+kk; if(sm<sum) { sum = sm; res[0]=ii; res[1]=jj; res[2]=kk; res[3]=div[0]; } } int main() { freopen("ratios.in", "r", stdin); freopen("ratios.out", "w", stdout); scanf("%d%d%d", &si[0], &si[1], &si[2]); for(int i=0; i<3; i++) for(int j=0; j<3; j++) scanf("%d", &a[i][j]); sum = 0x3fffffff; for(int i=0; i<=100; i++) for(int j=0; j<=100; j++) for(int k=0; k<=100; k++) { if(i==0 && j==0 && k==0) continue; if(i==8 && j==1 && k==5) { int t; t = 0; } deal(i, j, k); } if(sum == 0x3fffffff) printf("NONE\n"); else printf("%d %d %d %d\n", res[0], res[1], res[2], res[3]); return 0; }
时间: 2024-10-13 12:05:05