#include<iostream>
using namespace std;
bool test01(string str1, string str2)
{
//首先判断两个字符串的大小是否一样
if (str1.size() != str2.size())
{
return false;
}
//如果两个字符串重新排列后可能一样,那其中的元素也必须一样
for (int i = 0; i < str1.size(); i++)
{
bool a = 0;//默认某个元素str2没有
for (int j = 0; j < str2.size(); j++)
{
if (str1[i] == str2[j])
{
a = 1;
}
}
//若字符串str1中的某个元素str2没有,那就不可能重复
if (!a)
{
return 0;
}
}
return 1;
}
int main()
{
string str1;
string str2;
cout << "请输入第一个字符串" << endl;
cin >> str1;
cout << "请输入第二个字符串" << endl;
cin >> str2;
bool outcome = test01(str1, str2);
if (outcome)
{
cout << "两个字符串可能相等" << endl;
}
else
{
cout << "两个字符串不相等" << endl;
}
system("pause");
return 0;
}
给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。(C++)
最新推荐文章于 2024-02-26 17:46:38 发布