游游的水果大礼包
题目链接:游游的水果大礼包__牛客网 (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] << ' ';
}