gpt4 book ai didi

java - SPOJ 问题 - 这里出了什么问题?

转载 作者:搜寻专家 更新时间:2023-10-31 00:45:37 25 4
gpt4 key购买 nike

我在回答这个 spoj 问题: http://www.spoj.pl/problems/JAVAC/

我用 C++ 编写了提交代码。我在下面粘贴它。提交始终给出错误的答案。我找不到它会失败的测试用例。谁能帮帮我?

非常感谢。

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

char *ans;

bool iscapital(char c)
{
if(c >='A' && c <='Z')
return true;
else
return false;
}

string translate(string current)
{
ans = new char[2*current.length() + 1];
int jflag = 0, cflag = 0,j = 0, i = 0;

if(iscapital(current[0]))
{
return "Error!";
}

while(i < current.length())
{
if(current[i] !='_')
{
if(!(current[i] >= 'A' && current[i] <= 'Z'))
{
ans[j] = current[i];
i++;
j++;
}
}

if(current[i] == '_')
{
if(jflag == 1)
return "Error!";

cflag = 1;
i++;
if(i < current.length())
{
//convert to capital
if(iscapital(current[i]))
return "Error!";

ans[j] = (char)((int)current[i] - 32);
i++;
j++;
}
}

if(iscapital(current[i]))
{
if(cflag == 1)
return "Error!";

jflag = 1;

ans[j] = '_';
j++;
ans[j] = (char)((int)current[i] + 32);
j++;
i++;
}
}
ans[j] = '\0';
string ret = ans;
return ret;
}



int main()
{

string current;

while(cin>>current)
{
cout<<translate(current)<<endl;
}
return 0;
}

最佳答案

我没试过(没用过SPOJ),会不会是其他格式不对?例如:

123asd

_asd

asd___a

我现在正在弄乱你的代码,看看我能否让它工作。祝你好运!

编辑:通过!尝试添加以下内容:

  1. 当第一个或最后一个字母是'_'时检测错误

  2. 检测是否有连续的'_(例如,'a__b')

  3. 检测其他字符(例如,'adq21#')

这里的三种情况应该都是错误的。干杯!

关于java - SPOJ 问题 - 这里出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com