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

· · 题解

简单模拟题,循环统计 1 的个数即可,注意下标从 0 开始。

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    int ans = 0;
    cin >> s;
    for(int i = 0; i < 8; i ++){
        if(s[i] == '1') ans ++;
    }
    cout << ans;
}