用于计算给定文本卷的 C++ 代码
假设我们有一个包含 n 个字符的字符串 S。S 是一个由小写和/或大写英文字母组成由空格分隔的单词。单词的音量是大写字母在该单词中出现的次数。文本的音量是文本中所有单词的最大音量。我们需要找到给定文本的音量。
因此,如果输入类似 S = "Paper MILL",则输出为 4,因为第一个单词的音量为 1,而第二个单词的音量为 4,所以最大值为 4。
步骤
为解决此问题,我们将按照以下步骤操作 −
ans := 0 a := 0 n := size of S for initialize i := 0, when i <= n, update (increase i by 1), do: s := S[i] if s >= 'A' and s <= 'Z', then: (increase a by 1) if s is same as blank space, then: ans := maximum of ans and a a := 0 ans := maximum of ans and a return ans
举例
让我们看看以下实现,以获得更好的理解 −
#include <bits/stdc++.h>
using namespace std;
int solve(string S){
int ans = 0, a = 0;
int n = S.size();
for (int i = 0; i <= n; i++){
char s = S[i];
if ((s >= 'A') && (s <= 'Z'))
a++;
if (s == ' '){
ans = max(ans, a);
a = 0;
}
}
ans = max(ans, a);
return ans;
}
int main(){
string S = "Paper MILL";
cout << solve(S) << endl;
}输入
"Paper MILL"
输出
4
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP