联系电话、微信:志成教育-赵老师:15588622005 (报名请说“西海岸学习吧”有惊喜礼品)

西海岸学习吧

题目描述

小 K 同学向小 P 同学发送了一个长度为 8 的 01 字符串来玩数字游戏,小 P 同学想要知道字符串中究竟有多少个 1

注意:01 字符串为每一个字符是 0 或者 1 的字符串,如“101”(不含双引号)为一个长度为 3 的 01 字符串。

输入格式

输入文件只有一行,一个长度为 8 的 01 字符串 

输出格式

输出文件只有一行,包含一个整数,即 01 字符串中字符 1 的个数。

输入输出样例

输入 #1

00010100

输出 #1

2

输入 #2

11111111

输出 #2

8

说明/提示

【输入输出样例 1 说明】

该 01 字符串中有 2 个字符 1

【输入输出样例 2 说明】

该 01 字符串中有 8 个字符 1

【数据规模与约定】

  • 对于 20% 的数据,保证输入的字符全部为 0
  • 对于 100% 的数据,输入只可能包含字符 0 和字符 1,字符串长度固定为 8

 

#include <bits/stdc++.h>

using namespace std;

string s;
int cnt;

int main(){
	cin>>s;
	for(int i=0;i<s.size();i++){ //s.size() 这个字符的长度
		if(s[i]=='1') cnt++;
	
	}
	cout<<cnt;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

string s;
int cnt;

int main(){
	cin>>s;
	for(int i=0;i<8;i++){
		if(s[i]=='1') cnt++;
	
	}
	cout<<cnt;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main(){
	char s[10]={0};
	int cnt=0;
	cin>>s;
	for(int i=0;s[i]!='\0';i++){
		if(s[i]=='1') {
			cnt++;	
		}	
	}		
	cout<<cnt;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main() {
	string s;
	int cnt=0;
	cin>>s;
	
	for (int i=0;i<s.length();i++) {   //s.length() 这个字符的长度
		if (s[i]=='1') cnt++;
	}
	cout<<cnt;

	return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注