C++ 为什么这个字谜代码不能只为ravi&;给出正确的结果;维拉?
在这个程序中,我试图通过将字符串作为一个整数,按其ASCII值对字符串进行排序。然后按升序对它们进行排序,并检查它们是否具有相同的元素集C++ 为什么这个字谜代码不能只为ravi&;给出正确的结果;维拉?,c++,c++14,C++,C++14,在这个程序中,我试图通过将字符串作为一个整数,按其ASCII值对字符串进行排序。然后按升序对它们进行排序,并检查它们是否具有相同的元素集 #include<iostream> #include<string.h> using namespace std; int len(int a[]){ int l=0; for(int i=0; a[i]!='\0'; i++) l++; return l; } int len_str
#include<iostream>
#include<string.h>
using namespace std;
int len(int a[]){
int l=0;
for(int i=0; a[i]!='\0'; i++)
l++;
return l;
}
int len_str(string a){
int l=0;
for(int i=0; a[i]!='\0'; i++)
l++;
return l;
}
void asc_order(int a[]){
int n=len(a), temp=0;
for(int i=0; i<n ; i++)
for(int j=0; j<n; j++)
if(a[i]<a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int main(){
system("cls");
string str1, str2;
cout<<"Enter the first string : ";
getline(cin, str1);
cout<<"Enter the second string : ";
getline(cin, str2);
string res = "YES";
if(len_str(str1)==len_str(str2)){
int n = len_str(str1);
int ch1[n], ch2[n];
for(int i=0; i<n; i++){
ch1[i] = str1[i];
ch2[i] = str2[i];
}
asc_order(ch1);
asc_order(ch2);
for(int i=0; i<n; i++)
if(ch1[i]!=ch2[i])
res = "NO";
}
else
res = "NO";
cout<<res;
}
#包括
#包括
使用名称空间std;
int len(int a[]{
int l=0;
对于(int i=0;a[i]!='\0';i++)
l++;
返回l;
}
内部长度(字符串a){
int l=0;
对于(int i=0;a[i]!='\0';i++)
l++;
返回l;
}
无效asc_订单(int a[]{
int n=len(a),temp=0;
对于(int i=0;i而言,问题实际上是这条线:
int ch1[n], ch2[n];
除此之外,C++是无效的(G++允许动态大小数组)它创建的是字符串长度,而不是ASCII的全部范围。字符串可能只有10个字符长,但是“A”的值为97。所以它超出了范围,并且进入了未定义的行为区域。此外,您不需要定义自己的长度函数。这已经内置到C++字符串类中。
更改此内容:
if(len_str(str1)==len_str(str2)){
int n = len_str(str1);
int ch1[n], ch2[n];
for(int i=0; i<n; i++){
ch1[i] = str1[i];
ch2[i] = str2[i];
}
asc_order(ch1);
asc_order(ch2);
for(int i=0; i<n; i++)
if(ch1[i]!=ch2[i])
res = "NO";
}
else
res = "NO";
if(len_str(str1)=len_str(str2)){
int n=长_str(str1);
int-ch1[n],ch2[n];
对于(inti=0;i,您的排序中有一个bug
我删除了大部分不必要的代码并保留了排序逻辑():
它可以工作:YES
您的asc\u顺序
函数将交换两个条目,无论它们的顺序是正确的还是错误的
for(int i=0; i<a.length() ; i++)
for(int j=0; j<a.length(); j++)
if(a[i]<a[j]){
int-ch1[n];
-s不是标准的C++\include
而不是\include
。您不使用任何C-string函数,而是使用std::string
。为什么使用len str(str1)
而不是str1.length()
?您不需要int-ch1[n],ch2[n]
。您可以std::sort(str1.begin),str1.end());
这段代码应该做什么?在我删除了所有不必要的东西后,它可以工作:非常感谢。我将数组的大小固定为100而不是n,它工作起来很有魅力!!为什么将数组限制为100而不是256可以解决任何问题?在字母d
处的任何字符(ascii值100)或更高版本出现在str1
或str2
中,表示您的程序已损坏。
#include <algorithm>
#include<iostream>
#include<string>
using namespace std;
void asc_order(string a){
char temp=0;
for(std::size_t i=0; i < a.length() ; i++)
for(std::size_t j=0; j < a.length(); j++)
if(a[i]<a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int main(){
string str1, str2;
cout<<"Enter the first string : ";
getline(cin, str1);
cout<<"Enter the second string : ";
getline(cin, str2);
string res = "YES";
if(str1.length() == str2.length()) {
asc_order(str1);
asc_order(str2);
for(std::size_t i=0; i < str1.length(); i++)
if(str1[i] != str2[i])
res = "NO";
}
else
res = "NO";
cout<<res;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
string str1, str2;
cout<<"Enter the first string : ";
getline(cin, str1);
cout<<"Enter the second string : ";
getline(cin, str2);
string res = "YES";
if(str1.length() == str2.length()) {
std::sort(str1.begin(), str1.end());
std::sort(str2.begin(), str2.end());
for(std::size_t i=0; i < str1.length(); i++)
if(str1[i] != str2[i])
res = "NO";
}
else
res = "NO";
cout<<res;
}
for(int i=0; i<a.length() ; i++)
for(int j=0; j<a.length(); j++)
if(a[i]<a[j]){
for(int i=0; i<a.length() ; i++)
for(int j=i; j<a.length(); j++)
if(a[i]<a[j]){