8 条题解

  • 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;
    }
    
    • 1
      @ 2026-10-6 8:54:14

      提供一种大炮打蚊子方法 用序列线段树或树状数组维护01数组 计算 1~n 总和 时间复杂度 O(n+logn) 建树 O(n),计算 O(log n)

      #include<bits/stdc++.h>
      using namespace std;
      const int inf=0;
      int tree[2000005];
      int a[2000005];
      int n,q;
      inline int f(int a,int b) {
      	return a+b;
      }
      inline void init(int p,int l,int r) {
      	if(l==r) tree[p]=a[l];
      	else {
      		int mid=(l+r)/2;
      		init(p*2,l,mid);
      		init(p*2+1,mid+1,r);
      		tree[p]=f(tree[p*2],tree[p*2+1]);
      	}
      }
      inline void replace(int x,int y) {
      	int posx=1,posy=n,pos=1;
      	while(posx<posy) {
      		if(x>(posx+posy)/2) {
      			posx=(posx+posy)/2+1;
      			pos=pos*2+1;
      		} else {
      			posy=(posx+posy)/2;
      			pos*=2;
      		}
      	}
      	tree[pos]+=y;
      	pos>>=1;
      	while(pos>0) {
      		tree[pos]=f(tree[pos*2],tree[pos*2+1]);
      		pos>>=1;
      	}
      }
      inline int cal(int l,int r,int x,int y,int p) {
      	if(x>=l&&y<=r) return tree[p];
      	else if(x>r||l>y) {
      		return inf;
      	} else {
      		int left = cal(l,r,x,(x+y)/2,p*2);
      		int right = cal(l,r,(x+y)/2+1,y,p*2+1);
      		return f(left,right);
      	}
      }
      int main() {
      	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
      	string s;
      	cin>>s;
      	n=s.size();
      	for(int i = 1;i<=n;i++) {
      		a[i]=(s[i-1]=='1');
      	}
      	init(1,1,n);
      	cout<<cal(1,n,1,n,1);
      	return 0;
      }
      
      • 1
        @ 2026-10-6 8:33:59

        A. 数字游戏 · 题解

        CSP-J 2019 第二轮认证

        解题思路

        把输入读成字符串,从左到右检查每个字符。遇到字符 1,计数器加一;遇到 0,计数器不变。

        例如 00110100 中有三个 1,答案就是 33。使用字符串读取可以自然保留开头的零。

        正确性与复杂度

        处理完前 ii 个字符后,计数器恰好等于这 ii 个字符中 1 的数量。每次检查保持这一性质,因此遍历结束时得到所求答案。

        字符串长度固定为 88,时间和空间复杂度均为 O(1)O(1)。

        参考代码(C++11)

        #include <bits/stdc++.h>
        using namespace std;
        int main() {
            string s; cin >> s;
            int ans = 0;
            for (char c : s) ans += (c == '1');
            cout << ans << '\n';
        }
        
        • 0
          @ 2026-10-6 8:59:56

          这一题其实只需要遍历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;//完结撒花
            
          }
          
          • 0
            @ 2026-10-6 8:57:41
            #include <bits/stdc++.h>
            using namespace std;
            int main(){
                string n;
                int cnt = 0;
                cin >> n;
                for(int i = 0; i < 8; i ++){
                    if(n[i] == '1') cnt ++;
                }
                cout << cnt << endl;
                return 0;
            }
            
            • 0
              @ 2026-10-6 8:50:59

              A. 数字游戏 · 题解 CSP-J 2019 第二轮认证

              解题思路 把输入读成字符串,从左到右检查每个字符。遇到字符 1,计数器加一;遇到 0,计数器不变。

              例如 00110100 中有三个 1,答案就是 33。使用字符串读取可以自然保留开头的零。

              正确性与复杂度 处理完前 ii 个字符后,计数器恰好等于这 ii 个字符中 1 的数量。每次检查保持这一性质,因此遍历结束时得到所求答案。

              字符串长度固定为 88,时间和空间复杂度均为 O(1)O(1)。

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

              • 0
                @ 2026-10-6 8:49:12

                您怎么这么强?!

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

                我好菜(╥﹏╥)

                • @ 2026-10-6 8:52:32

                  你们 怎么 都 这么强👍

              • 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
                难度
                5
                标签
                递交数
                70
                已通过
                29
                上传者