raysun007 @ 2023-02-08 00:13:38
两段完全相同的逻辑,采用的输入方式不同,导致AC和全WA两个完全不同的结果,请教各位洛谷同学
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<set>
#include<unordered_set>
#include<vector>
#include<climits>
#include<limits>
#include<map>
#include<unordered_map>
using namespace std;
int main() {
string s;
getline(cin, s);
int n = s.size();
for (int i = 1; i < n; i++) {
string str;
getline(cin, str);
s.append(str);
}
// cout << "s: " << s << " size: " << s.size() << endl;
vector<int> v;
int cnt = 0;
int i = 0;
if (s[0] == '1') {
v.push_back(0);
}
bool flag = false;
while (i < s.size()) {
if (s[i] != '0' && s[i] != '1') {
i++;
}
while (i < s.size() && s[i] == '0') {
i++;
cnt++;
flag = true;
}
if (flag) {
v.push_back(cnt);
flag = false;
cnt = 0;
}
while (i < s.size() && s[i] == '1') {
i++;
cnt++;
flag = true;
}
if (flag) {
v.push_back(cnt);
flag = false;
cnt = 0;
}
}
cout << n << ' ';
for (int i = 0; i < v.size() - 1; i++) {
cout << v[i] << ' ';
}
cout << v[v.size() - 1] << endl;
return 0;
}
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<set>
#include<unordered_set>
#include<vector>
#include<climits>
#include<limits>
#include<map>
#include<unordered_map>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
for (int i = 1; i < n; i++) {
string str;
cin >> str;
s.append(str);
}
vector<int> v;
int cnt = 0;
int i = 0;
if (s[0] == '1') {
v.push_back(0);
}
bool flag = false;
while (i < s.size()) {
while (i < s.size() && s[i] == '0') {
i++;
cnt++;
flag = true;
}
if (flag) {
v.push_back(cnt);
flag = false;
cnt = 0;
}
while (i < s.size() && s[i] == '1') {
i++;
cnt++;
flag = true;
}
if (flag) {
v.push_back(cnt);
flag = false;
cnt = 0;
}
}
cout << n << " ";
for (int i = 0; i < v.size() - 1; i++) {
cout << v[i] << " ";
}
cout << v[v.size() - 1] << endl;
return 0;
}
by Coffins @ 2023-02-08 07:26:15
@raysun007 Linux下换行不是'\n'(是啥我也不知道),所以getline会出大问题
by WangLianda @ 2023-02-08 09:29:22
好像是\r ?
by raysun007 @ 2023-02-08 22:54:56
确实是Mac系统, 有懂的大佬解答一下
by sxjsxj @ 2023-07-26 11:17:22
看一下别人这篇,无聊翻的时候发现的 getline全WA