题目链接;http://blog.csdn.net/csdn364988181/article/details/48253267
题意:
有一个字符串表示制作1个汉堡的菜单。第二行给出拥有的每种材料的个数。第三行给出每种材料的加钱。第四行给出有的钱。问至多能弄到多少个汉堡。
题解:
二分。注意要开longlong,上界也要开的足够大。
代码:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
typedef long long LL;
char menu[105];
int nb, ns, nc;
int pb, ps, pc;
int hb, hs, hc, len;
LL have;
LL l = 1LL, r = 2000000000000LL;
bool isok(LL num) {
LL tb, ts, tc;
tb = (num*hb-nb)*pb; ts = (num*hs-ns)*ps; tc = (num*hc-nc)*pc;
if(tb < 0) tb = 0; if(ts < 0) ts = 0; if(tc < 0) tc = 0;
return tb+ts+tc <= have;
}
int main() {
int i = 0;
scanf("%s %d %d %d %d %d %d %I64d", menu, &nb, &ns, &nc, &pb, &ps, &pc, &have);
len = strlen(menu);
for ( i = 0; i < len; i ++ ) {
if(menu[i] == 'B') hb++;
else if(menu[i] == 'S') hs ++;
else if(menu[i] == 'C') hc ++;
}
int ans = -1;
while ( l < r ) {
LL mid = l+r >> 1;
if( isok(mid) ) {
l = mid+1;
} else r = mid;
}
printf("%I64d\n", l-1);
return 0;
}