串的习题
1.Problem - 2030
#include<iostream>
using namespace std;
int main() {
char s[500];
int n;
cin >> n;
getchar();//去掉空格部分
while (n--) {
gets(s);//老式写法 vs显示错误题目解答正确
int cnt = 0;
int len = strlen(s);
for (int i = 0; i < len; ++i) {
if (s[i] < 0) {
++cnt;
}
}
cout << cnt / 2 << endl;
}
return 0;
}
2.Problem - 2026
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char s[110];
while (gets(s)) {
int len = strlen(s);
for (int i = 0; i < len; ++i) {
if (i == 0 || s[i - 1] == ' ') {
if (s[i] != ' ') {
if (s[i] >= 'a' && s[i] <= 'z') {
s[i] -= 'a';
s[i] += 'A';
}
}
}
}
printf("%s\n", s);
}
return 0;
}
3.Problem - 2025
#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
while(cin >> s) {
string ret = "";
char maxc = 'a';//定义最小字符元素
for (int i = 0; i < s.size(); ++i) {//遍历
if (s[i] > maxc) {
maxc = s[i];//更新得到最大s[i]
}
}
for (int i = 0; i < s.size(); ++i) {//遍历
ret = ret + s[i];//字符串加到ret上
if (s[i] == maxc) {
ret = ret + "(max)";//加上最大标记
}
}
cout << ret;
}
return 0;
}
4.1812. 判断国际象棋棋盘中一个格子的颜色 - 力扣(LeetCode)
class Solution {
public:
bool squareIsWhite(string coordinates) {
int x = coordinates[0]-'a';
int y = coordinates[1]-'1';
return (x + y) % 2 == 1;
}
};
5.LCR 122. 路径加密 - 力扣(LeetCode)
class Solution {
public:
string pathEncryption(string path) {
int len = path.size();
for(int i = 0;i<len;++i){
if(path[i] == '.'){
path[i] = ' ';
}
}
return path;
}
};
6.1876. 长度为三且各字符不同的子字符串 - 力扣(LeetCode)
class Solution {
public:
int countGoodSubstrings(string s) {
int len = s.size();
int cnt = 0;
for(int i =0;i<len-2;++i){//长度为3
int a = s[i];
int b = s[i+1];
int c = s[i+2];
if( a!= b&&b!=c&&a!=c){
++cnt;
}
}
return cnt;
}
};
7.LCP 17. 速算机器人 - 力扣(LeetCode)
class Solution {
public:
int calculate(string s) {
int len = s.size();
int x = 1 , y = 0;
for(int i = 0;i<len;++i){
if(s[i]=='A'){
x = 2 * x + y;
}else if(s[i]=='B'){
y = 2 * y + x;
}
}
return x+y;
}
};
8.2011. 执行操作后的变量值 - 力扣(LeetCode)
class Solution {
public:
int finalValueAfterOperations(vector<string>& operations) {
int X = 0;
//共同点 中间为+ 或者 中间为-
for(int i = 0;i<operations.size();++i){
string s = operations[i];
if(s[1]=='+'){
X++;
}else{
X--;
}
}
return X;
}
};