用於計算給定文字卷的 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