2 条题解

  • 2
    @ 2026-9-26 18:10:41

    题意

    给定一个字符串 ss ,求它含有多少 1 。

    思路

    遍历这个字符串,如果 sis_i 为 11 ,则 cnt++ 。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        string s;
        cin >> s;
        int cnt = 0;
        for (int i = 0;i < s.size();i++)
        {
            if (s[i] == '1'){cnt++;}
        }
        printf("%d",cnt);
        return 0;
    }
    
    • 0
      @ 2026-10-2 0:12:22

      题意

      给定一个长度为8的01字符串,求其中1的数量。

      解题思路

      01字符串中只有字符0和1,我们可以将每个读入的字符和1做与运算的值加进结果,重复8次后直接输出。

      代码实现

      #include<bits/stdc++.h>
      using namespace std;
      int main(){
          char o=48;//因为最多只有8个1所以输出只有一位数,故直接从'0'开始计数并输出
          for(char i=0;i<8;i++)o+=getchar()&1;
          putchar(o);
          return 0;
      }
      
      • 1

      信息

      ID
      2216
      时间
      1000ms
      内存
      256MiB
      难度
      8
      标签
      递交数
      11
      已通过
      7
      上传者