为什么这样写的代码会超时?

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

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

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

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

输入输出样例
输入 #1复制
00010100
输出 #1复制
2
输入 #2复制
11111111
输出 #2复制
8
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{char a;int b=0;cin.get(a);while(a!='\n'){if(a=='1') b++;cin.get(a);}cout<<b;}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char a[9] = {0};
    int b=0,i=0;
    cin>>a;
    while(a[i] != '\0')
    {
        if(a[i] = '1')
          b++;
        i++;
    }
    cout<<b;
    return 0;
}

cin.get(a);
改为
cin>>a;
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string a;
    int cnt = 0, i = 0;
    cin >> a;
    while (i < a.size())
    {
        if (a[i++] == '1') cnt++;
    }
    cout << cnt << endl;
    return 0;
}