笔试强训Day11 贪心 字符串

游游的水果大礼包

题目链接:游游的水果大礼包__牛客网 (nowcoder.com)

思路:直接枚举 i 个 a 礼包 , j 个 b 礼包 。 求最大值即可。

AC code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;

LL n, m, a, b;

int main() {
    cin >> n >> m >> a >> b;
    LL ans = 0;
    for (int i = 0; i * 2 <= n && i <= m; ++i) 
    {
        int j = min(n - 2 * i, (m - i) / 2);
        ans = max(ans, i * a + j * b);
    }
    cout << ans << endl;
    return 0;
}

买卖股票的最好时机 ii

题目链接:买卖股票的最好时机 ii__牛客网 (nowcoder.com)

思路:贪心问题, 要求的赚钱的最大值 我们肯定股票一涨 就卖 价格折线应该是递增的

AC code:

#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int n;
long long ans;
int a[N];
class Solution {
public:
    int maxProfit(int prices[])
    {
        int ans=0;
        for(int i = 0; i + 1 < n; i ++)
        {
            if(prices[i] < prices[i + 1])
                ans+=prices[i + 1]-prices[i];
        }
      return ans;
    }
};
int main() 
{
    cin>> n;
    for(int i = 0; i < n; i ++)
        cin >> a[i];
    
    int j = 0;

    
    cout << Solution().maxProfit(a) << endl;
    return 0;
    
}

OR62 倒置字符串

题目链接:倒置字符串_牛客题霸_牛客网 (nowcoder.com)

思路:

直接cin 输入字符串即可 遇到空格会停下来, 注意while(cin) 会多读一个空格。

AC code:

#include<string>
#include <iostream>
using namespace std;
const int N = 510;
string a[N];
string ans;
int main()
{
    int t = 0;

    while(cin >> a[t++]);

    for(int i = t - 2; i >= 0; i --)
         cout << a[i] << ' ';

}

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值