交替字符串 不知道哪里出了问题 QAQ

云之_彼端 2014-04-10 10:47:07
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
class HelloWord {
public:
static string getInfo(string str)
{
string str1, str2, str3;
bool bl = false;
cin >> str1;
cin >> str2;
cin >> str3;
int s1 = str1.size(), s2 = str2.size(), s3 = str3.size();
int i = 0, x = 0, y = 0;
if (s1 + s2 == s3 && (s1!=0 || s2!=0))
{
for (; i != s3; i++)
{
if (str3[i] == str1[x])
{
x++;
bl = true;
}
else if (str3[i] == str2[y])
{
y++;
bl = true;
}
else
{
bl = false;
str = "NO";
break;
}
}
if (bl)
{
str = "YES";
}
}
else
{
str = "NO";
}
return str;
}
};
int main()
{
string c = HelloWord::getInfo("Hello World");
cout << c << endl;
}
...全文
112 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
云之_彼端 2014-04-13
  • 打赏
  • 举报
回复
QAQ...................
AndyStevens 2014-04-11
  • 打赏
  • 举报
回复
有啥问题?还有你想实现什么效果?
logiciel 2014-04-11
  • 打赏
  • 举报
回复
试加2行:
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
class HelloWord {
public:
  static string getInfo(string str)
  {
    string str1, str2, str3;
    bool bl = false;
    cin >> str1;
    cin >> str2;
    cin >> str3;
    int s1 = str1.size(), s2 = str2.size(), s3 = str3.size();
    int i = 0, x = 0, y = 0;
    if (s1 + s2 == s3 && (s1!=0 || s2!=0))
    {
      for (; i != s3; i++)
      {
        if (str3[i] == str1[x])
        {
          x++;
          bl = true;
          if (x == s1) break;//加
        }
        else if (str3[i] == str2[y])
        {
          y++;
          bl = true;
          if (y == s2) break;//加
        }
        else
        {
          bl = false;
          str = "NO";
          break;
        }
      }
      if (bl)
      {
        str = "YES";
      }
    }
    else
    {
      str = "NO";
    }
    return str;
  }
};
int main()
{
  string c = HelloWord::getInfo("Hello World");
  cout << c << endl;
}
云之_彼端 2014-04-11
  • 打赏
  • 举报
回复
引用 1 楼 logiciel 的回复:
试加2行:
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
class HelloWord {
public:
  static string getInfo(string str)
  {
    string str1, str2, str3;
    bool bl = false;
    cin >> str1;
    cin >> str2;
    cin >> str3;
    int s1 = str1.size(), s2 = str2.size(), s3 = str3.size();
    int i = 0, x = 0, y = 0;
    if (s1 + s2 == s3 && (s1!=0 || s2!=0))
    {
      for (; i != s3; i++)
      {
        if (str3[i] == str1[x])
        {
          x++;
          bl = true;
          if (x == s1) break;//加
        }
        else if (str3[i] == str2[y])
        {
          y++;
          bl = true;
          if (y == s2) break;//加
        }
        else
        {
          bl = false;
          str = "NO";
          break;
        }
      }
      if (bl)
      {
        str = "YES";
      }
    }
    else
    {
      str = "NO";
    }
    return str;
  }
};
int main()
{
  string c = HelloWord::getInfo("Hello World");
  cout << c << endl;
}
诶? 我那个 if else if else 不是一组的吗?? 只会执行其中一个的吧? 唔...
云之_彼端 2014-04-11
  • 打赏
  • 举报
回复
引用 2 楼 Automation_dmu 的回复:
有啥问题?还有你想实现什么效果?
就是那个编程挑战里的 交替字符串~

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧