今天回学校了,一整天坐车啊,走路啊,累得半死,算了,发完这题就睡觉吧。 首先,我的思路就是,mid表示的是最多的可以做的面包的数目,然后通过mid来求价格即可,将这个价格和money相比,然后不断地二分法即可。 #include<cstdio> #include<algorithm> #include<cstring> #define inf 1e13 using namespace std; typedef long long ll; char s[100 + 5]; ll num[4]; ll n[4], p[4]; ll money; void init(){ for (int i = 0; s[i] != '\0'; i++){ if (s[i] == 'B') num[0]++; else if (s[i] == 'S') num[1]++; else num[2]++; } for (int i = 0; i < 3; i++){ scanf("%I64d", n + i); } for (int i = 0; i < 3; i++){ scanf("%I64d", p + i); } scanf("%I64d", &money); } bool C(ll x){ ll price = 0; for (int i = 0; i < 3; i++){ if (x * num[i] > n[i]) price += p[i] * (x * num[i] - n[i]); } if (price <= money) return true; return false; } void solve(){ ll lb = 0, ub = inf; while (ub - lb > 1){ ll mid = lb + (ub - lb) / 2; if (C(mid)) lb = mid; else ub = mid; } printf("%I64d\n", lb); } int main(){ scanf("%s", s); init(); solve(); return 0; } </cstring></algorithm></cstdio>