当前位置: 首页> 财经> 访谈 > 每日OJ_牛客HJ74 参数解析

每日OJ_牛客HJ74 参数解析

时间:2025/7/18 4:11:15来源:https://blog.csdn.net/GRrtx/article/details/140859388 浏览次数:0次

目录

牛客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;
}
关键字:每日OJ_牛客HJ74 参数解析

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: