解题思路

这是一个简单的字符统计问题,需要统计字符串中大写字母('A'-'Z')的个数。

  1. 处理输入:

    • 读取一行字符串
    • 字符串长度范围:
  2. 统计大写字母:

    • 遍历字符串的每个字符
    • 判断是否是大写字母('A'-'Z')
    • 累计大写字母的个数
  3. 输出结果:

    • 输出统计到的大写字母个数

代码

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    getline(cin, str);
    
    int count = 0;
    for(char c : str) {
        if(c >= 'A' && c <= 'Z') {
            count++;
        }
    }
    
    cout << count << endl;
    return 0;
}
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        
        int count = 0;
        for(char c : str.toCharArray()) {
            if(c >= 'A' && c <= 'Z') {
                count++;
            }
        }
        
        System.out.println(count);
    }
}
s = input()
count = sum(1 for c in s if c.isupper())
print(count)

算法及复杂度

  • 算法:字符串遍历
  • 时间复杂度: - 其中 为字符串长度,需要遍历整个字符串
  • 空间复杂度: - 需要存储输入的字符串