目录
牛客HJ74 参数解析
解析代码1
解析代码2
牛客HJ74 参数解析
参数解析_牛客题霸_牛客网
解析代码1
本题通过以空格和双引号为间隔,统计参数个数。对于双引号,通过添加flag,保证双引号中的空格被输出。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{string str = "";getline(cin, str);vector<string> res;if (str.size() == 0 || str[0] == ' ')return 0;string tmp;for (int i = 0; i < str.size(); ++i){if(str[i] == '"'){while(str[++i] != '"'){tmp += str[i];}res.push_back(tmp);tmp = "";i++; // 跳过一个双引号后的空格}else if (str[i] != ' '){tmp += str[i];}else{res.push_back(tmp);tmp = "";}}if(!tmp.empty())res.push_back(tmp);cout << res.size() << endl;;for (auto& e : res){cout << e << endl;}return 0;
}
解析代码2
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{string str = "";getline(cin, str);vector<string> res;if (str.size() == 0 || str[0] == ' ')return 0;string tmp;bool flag = false;for (int i = 0; i < str.size(); ++i){if(str[i] == '"') // 如果是双引号,让标志换值{flag = !flag;}else if (str[i] != ' ' || flag) {tmp += str[i];}else{res.push_back(tmp);tmp = "";}}if(!tmp.empty())res.push_back(tmp);cout << res.size() << endl;;for (auto& e : res){cout << e << endl;}return 0;
}