题目描述:让我们求一个长度为 的 01 字符串中有多少个 。
思路:无需多言,直接字符串遍历,遍历字符串的每一位字符,统计字符 '1' 出现的次数,最后输出统计结果。
代码:
#include <bits/stdc++.h>
using namespace std;
int main(){
//别忘了加freopen!
int cnt = 0;
string a;
cin >> a;
for(int i = 0;i<a.size();i++){ //遍历
if(a[i] == '1'){ //判断是否为1
cnt++;
}
}
cout << cnt;
return 0;
}
暂无评论