跳到正文

题解

题解:P5660 [CSP-J2019] 数字游戏

2huk约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年12月21日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 1 分钟
获赞
1
题目
P5660

版本

  1. v1当前

统计字符串中某种字符的出现次数,可以用 STL 的 count 函数。

C++
#include <bits/stdc++.h>

using namespace std;

signed main() {
	string s;
	cin >> s;
	cout << count(s.begin(), s.end(), '1');
	return 0;
}

或者使用 count_if 函数。

C++
#include <bits/stdc++.h>

using namespace std;

signed main() {
	string s;
	cin >> s;
	cout << count_if(s.begin(), s.end(),
		[&](char c) {
			return c == '1';
		});
	return 0;
}
原文