BC146 添加逗号
题目链接:添加逗号_牛客题霸_牛客网 (nowcoder.com)
思路:倒着读一遍 每隔三个字符加一个逗号 即可
AC code:
#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
string a,b;
int main()
{
cin >> a;
int cnt = 0;
for(int i = a.size() - 1; i >= 0; i --)
{
if(cnt % 3 == 0 && cnt)
{
b += ',', b += a[i];
}
else b += a[i];
cnt ++;
}
reverse(b.begin(), b.end());
cout << b << endl;
}
DP2 跳台阶
题目链接:跳台阶_牛客题霸_牛客网 (nowcoder.com)
思路:类斐波那契数列 每一项的状态由前两项 决定 , 取和。数据量小
直接预处理一遍输出结果即可。
AC code:
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 50;
int n;
LL dp[N] = {1,2};
int main()
{
for(int i = 2; i < 45; i ++)
dp[i] = dp[i - 1] + dp[i - 2];
cin >> n;
cout << dp[n - 1];
}
JZ61 扑克牌顺子
题目链接:扑克牌顺子_牛客题霸_牛客网 (nowcoder.com)
思路:模拟题 五张牌 统计0的个数 记录最大值最小值 用 bool 数组 查询是否能凑齐
[left, right] 区间内的所有牌。
注意特判
① 有四张 0 直接 return 1 即可;
② 有两张相同的牌 直接 return 0 即可
AC code:
class Solution {
public:
bool IsContinuous(vector<int>& numbers)
{
bool st[20];
for(int i = 0; i <= 19; i ++) st[i] = 0;
int cnt = 0;
int right = -99;
int left = 99;
for(int i = 0; i < numbers.size(); i ++)
{
if(!numbers[i])
{
cnt ++; continue;
}
if(st[numbers[i]]) return 0;
st[numbers[i]] = 1;
left = min(left, numbers[i]);
right = max(right, numbers[i]);
}
if(cnt == 4) return true;
for(int i = left; i <= right; i ++)
{
if(!st[i] && cnt)
{
st[i] = 1; cnt --;
}
if(!st[i])
return 0;
}
return 1;
}
};