Z09103839 @ 2024-09-08 11:12:40
#include<iostream>
#include<string>
using namespace std;
int n,cnt=1;
int main() {
//链接所有字符串
string s,str;
while (cin>>s) str += s;
//输出矩阵长度
n = sqrt(str.size());
cout << n << ' ';
//与上一个比较
for (int i = 1; i < n * n; i++) {
if (str[i] == str[i-1]) cnt++;
else {
cout << cnt << ' ';
cnt = 1;
}
}
cout << cnt;
return 0;
}
案例和我自己输入的数都能得到正确答案啊orz
by Z09103839 @ 2024-09-08 14:15:02
找到问题了,是我审题不清,第一个数必须是0的个数```cpp
using namespace std; int n, cnt = 1; int main() { //链接所有字符串 string s, str; while (cin >> s) str += s; //输出矩阵长度 n = sqrt(str.size()); cout << n << ' '; //与上一个比较 if (str[0] != '0') cout << "0 "; for (int i = 1; i < n * n; i++) { if (str[i] == str[i - 1]) cnt++; else { cout << cnt << ' '; cnt = 1; } } cout << cnt; return 0; }
添加一行代码就可以AC了
by Z09103839 @ 2024-09-08 14:16:14
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int n, cnt = 1;
int main() {
//链接所有字符串
string s, str;
while (cin >> s) str += s;
//输出矩阵长度
n = sqrt(str.size());
cout << n << ' ';
//与上一个比较
if (str[0] != '0') cout << "0 ";
for (int i = 1; i < n * n; i++) {
if (str[i] == str[i - 1]) cnt++;
else {
cout << cnt << ' ';
cnt = 1;
}
}
cout << cnt;
return 0;
}