66kkCED @ 2023-01-07 11:34:34
以下是代码,求大佬解答
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
char compress[50000];
char ch;
int i;
int flag = scanf("%c",&ch);
compress[0] = ch;
for(i=1;flag!=EOF;i++)
{
flag = scanf("%c",&ch);
if(ch == '\n') i--;
else compress[i] = ch;
}
int n = sqrt(i);
cout<<n<<" ";
char op = '0';
int cnt = 0;
for(i=0;i<n*n;i++)
{
if(compress[i] == op) cnt++;
else
{
cout<<cnt<<" ";
cnt = 1;
if(op == '1') op = '0';
else op = '1';
}
}
cout<<cnt;
return 0;
}
by DreamLand_zcb @ 2023-01-07 11:54:16
@66kkCED 试试这个?
n=strlen(compress);
for(int i=0,cnt=0,op='0';i<=n;i++)
{
if(compress[i] ==op) cnt++;
else
{
op=compress[i];
cout << cnt << " ";
cnt=1;
}
}
by 66kkCED @ 2023-01-07 17:05:02
@DreamLand_zcb
奇怪,还是不太行
没弄清楚这题是有什么输入输出限制么?
by DreamLand_zcb @ 2023-01-07 18:17:02
@66kkCED 你把输入再改一下试试?
char last[40000], now[200];
cin >> now;
n=strlen(now);
strcat(last,now);
for(int i=2;i<=n;i++)
{
cin >> now;
strcat(last,now);
}
cout << n << " ";
by DreamLand_zcb @ 2023-01-07 18:17:44
@66kkCED 这是我的代码
n=strlen(last);
for(int i=0,sum=0,num='0';i<=n;i++)
{
if(last[i] == num)
sum++;
else
{
num=last[i];
cout << sum << " ";
sum=1;
}
}
by 66kkCED @ 2023-01-08 16:35:23
@DreamLand_zcb
嗯,AC了,感谢大佬!
ps.看来这个题输入单字符读入会出错,得使用字符串读入了