LiamZou @ 2024-01-30 20:34:48
#include <bits/stdc++.h>
using namespace std;
char a, pos = '0';
int num, cnt, tmps;
int ans[40005];
int main()
{
while (scanf("%d", &a) != EOF)
{
num++;
if (a == pos)
{
cnt++;
}
else
{
pos = a;
ans[tmps] = cnt;
cnt = 1;
tmps++;
}
}
ans[tmps] = cnt;
cout << sqrt(num) << " ";
for (int i = 0; i <= tmps; i++)
{
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
by jkluio1 @ 2024-03-09 17:05:36
@LiamZou
int main()
{
char a[20000];
int n=0, i = 0, cent[200] = { 0 }, j = 0, k = 0;
while (cin >> a[i++]);//输入所有元素
n = sqrt(i);//计算出数组的长度n;
if (a[0] == '1')//跳过第一个字符为‘1’的情况
{
cent[0] = 0;//将计数数组第一个元素设置为0
k++;
}
while (j < i)
{
if ((a[j] != a[j - 1] && j != 0))//字符变化则计数数组开始下一个元素的计数,j!=0避免数组越界
k++;
cent[k]++;
j++;//遍历
}
cout << n << " ";
for (int s = 0; s < k; s++)
cout << cent[s] << " ";
return 0;
}
有错艾特我,我也是小趴菜可能不太严谨哈哈哈
by LiamZou @ 2024-03-10 10:04:21
@jkluio1 太牛了,谢谢终于AC了!