这一题其实只需要遍历s字符串求里面有多少个一
#include<bits/stdc++.h> using namespace std; int main() { string s;//字符串s int cnt = 0;//一开始只有0个1 cin >> s;//输入 for (int i = 0; i < 8; i++)//遍历字符串 if (s[i] == '1') //判断第i个字符是不是1 cnt++;//计数器加一 cout << cnt << endl;//输出 return 0;//完结撒花 }
使用您的 深圳实验中学 OJ 通用账户