5 solutions

  • 1
    @ 2026-3-31 18:10:58
    #include <bits/stdc++.h>
    #define int long long 
    using namespace std;
    int n,n2,n3;
    signed main(){ 
    	cin>>n;
    	while(n){
    		if(n%10==1) n2++;
    		n/=10;
    	}
    	cout<<n2;
    	return 0;
    }
    
    
    
    • 1
      @ 2025-3-28 17:19:34

      简单易懂~~

      #include <bits/stdc++.h>
      #define int long long 
      using namespace std;
      int n,n2,n3;
      signed main(){ 
      	cin>>n;
      	while(n){
      		if(n%10==1) n2++;
      		n/=10;
      	}
      	cout<<n2;
      	return 0;
      }
      
      
      • 0
        @ 2025-8-11 14:27:41
        #include<bits/stdc++.h>
        using namespace std;
        int main() {
        	string s;
            cin>>s;
            int ans=0,n=s.size();
            s=" "+s;
            for (int i=1;i<=n;i++) ans+=s[i]-'0';
            cout<<ans;
        	return 0;
        }
        
        • 0
          @ 2025-7-25 16:27:26

          好的,我们看到这道题目首先看数据范围 字符串的长度仅仅有8位 so~~ 我们输入字符串后直接遍历查找是否为字符1 随之cnt+1(cnt记得清零) 还有,字符串数组范围是0~length-1

          #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++;
                  }
              }
              cout<<cnt;
              return 0;
          }
          

          希望本题解能帮助到你~

          • -1
            @ 2024-12-5 21:14:51

            原理就是输入这个字符串because他只有8位so就是从1~8挨个判断是不是1

            代码(code):

            #include<bits/stdc++.h>
            typedef long long ll;
            using namespace std;
            const int N=10010;
            string a;
            int cnt; 
            int main(){
            
            	cin.sync_with_stdio(false);
            	ios::sync_with_stdio(0);
            	cin.tie(0);cout.tie(0);
            	getline(cin,a);
            	for(int i=0;i<8;i++){
            		if(a[i]=='1'){
            			cnt++;
            		}
            	}
            	cout<<cnt;
            
            
            	return 0;
            }
            
            
            • 1

            Information

            ID
            51
            Time
            1000ms
            Memory
            256MiB
            Difficulty
            3
            Tags
            # Submissions
            53
            Accepted
            31
            Uploaded By