题解 | #参数解析#
参数解析
http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
答案是正确的,但最后一个用例通不过,不知道什么原因。
#include<iostream>
#include<vector>
using namespace std;
int main() {
string a;
getline(cin, a);
int b = a.length();
int d = 0;
vector<string>e;
while (1) {
int c = 0;
for (int i = 0; i < b + 1; i++) {
if (a[i] == ' ' || a[i] == '"') {
if (a[i] == '"' && i == 0) {
for (int j = i + 1; j < b; j++) {
if (a[j] == '"' && j < b - 1) {
c = 1;
d++;
string g = a.substr(1, j - 1);
e.push_back(g);
a = a.substr(j + 2, b - j - 2);
b = a.length();
i = -1;
} else if (a[j] == '"' && j == b - 1) {
c = 1;
string g = a.substr(1, j - 1);
e.push_back(g);
a = {NULL};
b = a.length();
i = -1;
}
}
} else if (a[i] == ' ') {
c = 1;
d++;
e.push_back(a.substr(0, i));
a = a.substr(i + 1, b - i - 1);
b = a.length();
i = -1;
}
}
}
if (c == 0) {
break;
}
}
cout << d + 1 << endl;
for (auto x : e) {
cout << x << endl;
}
if(!a.empty())
cout << a;
return (0);
}
#include<vector>
using namespace std;
int main() {
string a;
getline(cin, a);
int b = a.length();
int d = 0;
vector<string>e;
while (1) {
int c = 0;
for (int i = 0; i < b + 1; i++) {
if (a[i] == ' ' || a[i] == '"') {
if (a[i] == '"' && i == 0) {
for (int j = i + 1; j < b; j++) {
if (a[j] == '"' && j < b - 1) {
c = 1;
d++;
string g = a.substr(1, j - 1);
e.push_back(g);
a = a.substr(j + 2, b - j - 2);
b = a.length();
i = -1;
} else if (a[j] == '"' && j == b - 1) {
c = 1;
string g = a.substr(1, j - 1);
e.push_back(g);
a = {NULL};
b = a.length();
i = -1;
}
}
} else if (a[i] == ' ') {
c = 1;
d++;
e.push_back(a.substr(0, i));
a = a.substr(i + 1, b - i - 1);
b = a.length();
i = -1;
}
}
}
if (c == 0) {
break;
}
}
cout << d + 1 << endl;
for (auto x : e) {
cout << x << endl;
}
if(!a.empty())
cout << a;
return (0);
}