yang1812 @ 2023-11-20 20:16:01
#include<bits/stdc++.h>
using namespace std;
int a[40001];
int main(){
char c,l = '0';
int len = 0;
int ans = 0;
while(cin>>c){
len++;
if(c == l){
a[ans]++;
}else{
ans++;
a[ans] = 1;
l = c;
}
}
cout<<sqrt(len);
for(int i = 0;i <= ans;i++){
cout<<" "<<a[i];
}
return 0;
}
by LuoxiDream @ 2023-11-25 08:40:07
是不是没有判断什么时候跳出循环?
可以先输入一行数据,根据其长度来判断一共有多少行。
如下代码:
#include<bits/stdc++.h>
using namespace std;
int ans1; int a[40020];
string tmp;
int main(){ char c,l = '0'; int len; int ans = 0;
cin >> tmp;
ans1 = len = tmp.size();
for(int i = 0; i < len; i++) {
c = tmp[i];
if(c == l){
a[ans]++;
}else{
ans++;
a[ans] = 1;
l = c;
}
}
while(cin>>c){
len++;
if(c == l){
a[ans]++;
}else{
ans++;
a[ans] = 1;
l = c;
}
if(len == ans1 * ans1) break;
}
cout<<ans1;
for(int i = 0;i <= ans;i++){
cout<<" "<<a[i];
}
return 0;
}