c++输入一个字符串s判断其中包含了多少个0。输入 一个字符串s。输出 一个整数表示这个字符串中0的个数。输入样例 20200125输出样例 3用时内存 1000MS100MB
可以使用循环遍历字符串的每个字符,判断是否为'0',如果是则计数器加1。最后输出计数器的值即可。
下面是一个示例的C++代码实现:
#include <iostream>
#include <string>
int countZeros(const std::string& s) {
int count = 0;
for (char c : s) {
if (c == '0') {
count++;
}
}
return count;
}
int main() {
std::string s;
std::cin >> s;
int zeros = countZeros(s);
std::cout << zeros << std::endl;
return 0;
}
输入样例:
20200125
输出样例:
3
原文地址: https://www.cveoy.top/t/topic/igvO 著作权归作者所有。请勿转载和采集!