SP1163 JAVAC - Java vs C ++
传送门:SP1163 JAVAC - Java vs C ++
很明显一字符串模拟题,但是要考虑几个问题,这是翻译没有写的,首先如果 _
后面还是 _
,要输出 Error!
;第二,如果字符串末尾或第一个是 _
,也要输出 Error!
;最后,如果字符串第一个是大写,也要输出 Error!
。
然后就是模拟了:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
while (cin >> s)
{
if (s[0] == '_' || isupper(s[0]) || s.back() == '_')
{
cout << "Error!" << endl;
continue;
}
int len = s.length() - 1;
string copy = s;
transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
int fd = s.find("_");
string finalans = "";
if (fd != string::npos && copy != s)
{
cout << "Error!\n";
continue;
}
if (fd != string::npos)
{
for (int i = 0; i <= len; i++)
{
if (s[i] == '_')
{
if (isupper(s[i + 1]) || s[i + 1] == '_')
{
finalans = "Error!";
break;
}
else
{
s[i + 1] = toupper(s[i + 1]);
}
}
else
{
finalans += s[i];
}
}
}
else
{
for (int i = 0; i <= len; i++)
{
if (isupper(s[i]))
{
finalans += "_";
s[i] = tolower(s[i]);
}
finalans += s[i];
}
}
cout << finalans << endl;
}
return 0;
}
分类:
题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现