A. Guest From the Past 贪心

题目:
n n n 元钱,然后有塑料瓶的牛奶 a a a 元钱,玻璃瓶的牛奶 b b b 元钱,玻璃瓶可以卖 c c c元钱。问 n n n 元钱最多喝多少瓶牛奶。

思路:
如果一种样式的牛奶贡献大,那我们就一直喝这个到不能在喝为止,剩下的钱看看还能否喝其他种的牛奶即可,呢么存在两种情况,全喝 a a a 牛奶,剩余的钱只能喝 b b b,反则全喝 b b b,剩下的钱全喝 a a a 即可。

参考代码:

#include<iostream>
#include<vector>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int N=5e2+5;
int read(int &v)
{
    
    
    int k = 1;
    v = 0;
    int c = getchar();
    while (c < '0' || c > '9')
    {
    
    
        if (c == '-')
            k = 0;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        v = (v << 3) + (v << 1) + (c - 48), c = getchar();
    if (k == 0)
        v = -v;
    return c;
}
ll ksm(ll a,ll b)
{
    
    
    ll res=1;
    while(b)
    {
    
    
        if(b&1)
            res*=a;
        b>>=1,a=a*a;
    }
    return res;
}
ll solve(ll n,ll b,ll c)
{
    
    
    ll ans=0;
    while(n>=b)
    {
    
    
        ans+=n/b;
        n=n-(n/b)*b+(n/b)*c;
    }
    return ans;
}
int main()
{
    
    
    ll n,a,b,c;
    scanf("%lld %lld %lld %lld",&n,&a,&b,&c);
    if(n<a&&n<b)
    {
    
    
        puts("0");
        return 0;
    }
    ll ans=(n/a);
    ll res=0;
    if(n>=b)
    {
    
    
        res=(n-c)/(b-c);
        res+=(n-(b-c)*res)/a;
    }
    if(ans<res)
        printf("%lld\n",res);
    else
        printf("%lld\n",ans);
}

猜你喜欢

转载自blog.csdn.net/yangzijiangac/article/details/109090873