#include <bits/stdc++.h>
using namespace std;
int main()
{
string str, ret;
getline(cin, str);
char ch = str[0];
if (ch >= 'a' && ch <= 'z') ch -= 32;
ret += ch;
for (int i = 1; i < str.size(); i++)
{
if (str[i] == ' ')
{
char ch = str[i + 1];
if (ch >= 'a' && ch <= 'z') ch -= 32;
ret += ch;
}
}
cout << ret << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
while (cin >> str)
{
char ch = str[0];
if (ch >= 'a' && ch <= 'z') ch -= 32;
cout << ch;
}
return 0;
}
我还是太菜了,这题做了十多分钟…
#include <iostream>
using namespace std;
int main()
{
float a;
char b;
cin >> a >> b;
int ret = 20; // 起步价20
if (b == 'y') ret += 5;
if (a > 1)
{
a--;
while (a > 0)
{
ret++;
a--;
}
}
cout << ret;
return 0;
}
#include <iostream>
using namespace std;
int arr[101][101];
int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
int main()
{
int n, m;
cin >> n >> m;
int a = 0, b = 0, d = 1;
for (int i = 1; i <= n * m; i++)
{
arr[a][b] = i;
int x = a + dx[d], y = b + dy[d];
// 如果走到边界,或者该位置已经填过了,此时改变方向
if (x < 0 || x == n || y < 0 || y == m || arr[x][y])
{
d = (d + 1) % 4;
x = a + dx[d], y = b + dy[d];
}
a = x, b = y;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cout << arr[i][j] << " ";
cout << endl;
}
return 0;
}
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
int m = matrix.size(), n = matrix[0].size();
vector<int> ret(m * n);
vector<vector<bool>> used(m, vector<bool>(n));
int a = 0, b = 0, d = 1; // 开始的方向是向右
for (int i = 0; i < m * n; i++)
{
ret[i] = matrix[a][b];
used[a][b] = true;
int x = a + dx[d], y = b + dy[d];
if (x < 0 || x == m || y < 0 || y == n || used[x][y])
{
d = (d + 1) % 4;
x = a + dx[d], y = b + dy[d];
}
a = x, b = y;
}
return ret;
}
};
先把所有的数加起来,在这个过程之中把偶数放到堆中,在遍历这个全是偶数的堆k次,每次让所有数之和减去最大偶数的一半,如果最大偶数除2后还是偶数还要重新添加到堆中,在这个过程中还要关注堆是否已经空了。
这道题还需要关注数据范围的问题,很明显需要用到 long long 类型。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
priority_queue<ll> q;
ll n, k, t, sum = 0;
int main()
{
cin >> n >> k;
while (n--)
{
cin >> t;
sum += t;
// 把偶数存进堆
if (t % 2 == 0) q.push(t);
}
while (!q.empty() && k--)
{
t = q.top() / 2;
q.pop();
sum -= t;
if (t % 2 == 0) q.push(t);
}
cout << sum << endl;
return 0;
}
class Solution {
public:
string modifyString(string s) {
int n = s.size();
for (int i = 0; i < n; i++)
if (s[i] == '?')
for (char ch = 'a'; ch <= 'z'; ch++)
if ((i == 0 || s[i - 1] != ch) && (i == n - 1 || s[i + 1] != ch))
s[i] = ch;
return s;
}
};
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
int ret = duration;
for (int i = 1; i < timeSeries.size(); i++)
{
int tmp = timeSeries[i] - timeSeries[i - 1];
if (tmp >= duration) ret += duration;
else ret += tmp;
}
return ret;
}
};
画图,找规律、然后模拟实现。
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1) return s;
int n = s.size();
int d = 2 * numRows - 2;
string ret;
// 1.处理第一行
for (int i = 0; i < n; i += d)
ret += s[i];
// 2.处理第k行
for (int k = 1; k < numRows - 1; k++)
{
for (int i = k, j = d - k; i < n || j < n; i += d, j += d)
{
if (i < n) ret += s[i];
if (j < n) ret += s[j];
}
}
// 3.处理最后一行
for (int i = numRows - 1; i < n; i += d)
ret += s[i];
return ret;
}
};
硬模拟,毫无技巧可言。
class Solution {
public:
string countAndSay(int n) {
string ret("1");
while(--n)
{
string tmp;
for (int l = 0, r = 0; r < ret.size();)
{
while (ret[l] == ret[r]) r++;
tmp += to_string(r - l) + ret[l];
l = r;
}
ret = tmp;
}
return ret;
}
};
class Solution {
public:
int minNumberOfFrogs(string croakOfFrogs) {
string str("croak");
int n = str.size();
vector<int> hash(n);
unordered_map<char, int> index(n); // 绑定字符和下标
for (int i = 0; i < n; i++) index[str[i]] = i;
for (auto ch : croakOfFrogs)
{
if (ch == 'c')
{
if (hash[n - 1]) hash[n - 1]--;
hash[0]++;
}
else
{
int i = index[ch];
if (hash[i - 1] == 0) return -1;
hash[i - 1]--;
hash[i]++;
}
}
for (int i = 0; i < n - 1; i++)
if (hash[i]) return -1;
return hash[n - 1];
}
};
#include <iostream>
using namespace std;
int main()
{
int t; cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
string str; cin >> str;
int ret = 0, count = 0;
for (auto ch : str)
{
if (ch == 'W' && count >= 2) ret += k;
else if (ch == 'W') ret++, count++;
else ret--, count = 0;
}
cout << ret << endl;
}
return 0;
}
本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有