只有……20分

P1320 压缩技术(续集版)

y6hz @ 2023-05-02 22:37:55

code:

#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
int n, sum0 = 0, sum1 = 0, i = 0;
string a;
int main()
{
    cin >> a;
    int q = a.length();
    for (i; i < q - 1; i++)
    {
        string temp;
        cin >> temp;
        a += temp;
    }
    cout << q << ' ';
    for (i = 0; i < pow(q, 2); i++)
    {
        if (a[i] == '0')
        {
            sum0++;
            if (a[i + 1] == '1' || a[i + 1] == '\0')
            {
                cout << sum0 << ' ';
                sum0 = 0;
            }
        }
        else
        {
            sum1++;
            if (a[i + 1] == '0' || a[i + 1] == '\0')
            {
                cout << sum1 << ' ';
                sum1 = 0;
            }
        }
    }
}

by Lemonlwl @ 2023-05-02 23:23:45

第一个数表示连续有几个 0 0,第二个数表示接下来连续有几个 1 1,第三个数再接下来连续有几个 0 0,第四个数接着连续几个 1 1,以此类推……


by Lemonlwl @ 2023-05-02 23:26:43

@y6hz

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
string s,c;
int main(){
    cin>>c;
    s=c;
    int n=c.length(),sum=0;
    for(int i=1;i<n;i++){
        cin>>c;
        s+=c;
    }
    cout<<n<<" ";
    char last='0';
    for(int i=0;i<=s.length();i++){
        if(s[i]==last){
            sum++;
        }
        else{
            last=s[i];
            cout<<sum<<" ";
            sum=1;
        }
    }
    return 0;
}

你可以参考一下。


by Lemonlwl @ 2023-05-02 23:28:56

@y6hz 欢迎你加入这个。


by y6hz @ 2023-05-03 15:14:43

@Lemonlwl 谢谢,加了


|