为什么全是wa。。。我感觉没什么问题?!

P1320 压缩技术(续集版)

wuxinji @ 2022-11-14 19:05:47

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    string s,sb;
    int n = 0;
    while(getline(cin,sb)){
        s+=sb;  //合并成一个字符串
        n++;   //计数,求N
    }
    cout << n << ' ';

    int a = s[0],count = 0;
    for(auto i : s){
        if(i == a){
            count ++;
        }else if(i != a){
            cout << count << ' ';
            a = i;
            count = 1;
        }
    }
        cout << count ;
}

by Fatin @ 2022-12-18 10:23:38

getline读入改成cin >> sb

a初始类型改为char,赋值'0'

判断语句else if改为else

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    string s,sb;
    int n = 0;
    while(cin >> sb){
        s+=sb;  //合并成一个字符串
        n++;   //计数,求N
    }
    cout << n << ' ';
    char a = '0';
    int count = 0;
    for(auto i : s){
        if(i == a){
            count ++;
        }else{
            cout << count << ' ';
            a = i;
            count = 1;
        }
    }
    cout << count;
    return 0;
}

|