小红的笔记打卡
[题目链接](https://www.nowcoder.com/practice/b5d86fc39a074ca9b786d89a4bcde918)
思路
本题非常直接:给定一个长度为 7 的字符串,其中 'O' 表示当天发布了笔记,'X' 表示未发布,统计 'O' 出现的次数即可。
做法
- 读入字符串。
- 遍历每个字符,统计
'O'的个数。 - 输出计数结果。
复杂度分析
- 时间复杂度:
,其中
为字符串长度,遍历一次即可。
- 空间复杂度:
,只需一个计数变量。
代码
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int cnt = 0;
for (char c : s) {
if (c == 'O') cnt++;
}
cout << cnt << endl;
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'O') cnt++;
}
System.out.println(cnt);
}
}

京公网安备 11010502036488号