数字游戏(签到)
[题目链接]
https://www.luogu.com.cn/problem/P5660?contestId=146378
题意
找出输出的字符串(长度为八)中有多少个1
思路
- 定义一个字符串和1的次数
- 判断字符串中有几个1
- 输出次数
坑点
- 判断条件中1为字符,所以要加单引号‘ ’
算法一:用字符串,循环,判断
时间复杂度
入门
实现步骤
- 定义字符串
- for循环与判断
- 输出
代码
#include <iostream>
#include <string.h>
using namespace std;
int main(){
string s;
cin>>s;
int count;
for(int i=0;i<8;i++){
if(s[i]=='1'){
count++;
}
}
cout<<count;
return 0;
}
总结
本次主要考查字符串的知识点