【发布时间】:2013-07-02 02:59:04
【问题描述】:
我有这个代码来做一个字符串的排列。
#include <iostream>
#include <string.h>
using namespace std;
/* Prototipo de función */
void Permutaciones(char *, int l=0);
void sort(string scadena[]);
//array global to copy all permutations and later sort
string array[900000];
int m=0;
int main() {
int casos;
cin>>casos;
char palabra[casos][13];
for(int i=0;i<casos;i++)
cin>>palabra[i];
for(int i=0;i<casos;i++){
m=0;
Permutaciones(palabra[i]);
sort(array);
}
sort(array);
system("pause");
return 0;
}
void sort(string scadena[]){
string temp;
for(int i=0;i<m;i++){
for(int j=i+1;j<m;j++){
if(scadena[i]>scadena[j]){
temp=scadena[i];
scadena[i]=scadena[j];
scadena[j]=temp;
}
}
}
for(int i=0;i<m;i++){
for(int j=1;j<m;j++){
if(scadena[i]==scadena[j] && j!=i){
for(int k=j;k <m; k++){
scadena[k]=scadena[k+1];
}
m--;
j--;
}
}
}
for(int i=0;i<m;i++){
cout<<scadena[i]<<endl;
}
}
void Permutaciones(char * cad, int l) {
char c; /* variable auxiliar para intercambio */
int i, j; /* variables para bucles */
int n = strlen(cad);
for(i = 0; i < n-l; i++) {
if(n-l > 2){
Permutaciones(cad, l+1);
}
else {
array[m]=cad;
m++;
}
/* Intercambio de posiciones */
c = cad[l];
cad[l] = cad[l+i+1];
cad[l+i+1] = c;
if(l+i == n-1) {
for(j = l; j < n; j++){
cad[j] = cad[j+1];
}
cad[n] = 0;
}
}
}
代码生成所有排列都很好,后来对数组进行了排序,它工作正常。但是当我打算删除重复的字符串时,代码会显示一些重复的内容,而不是排序。
谁能告诉我我的错误是什么?
【问题讨论】:
-
使用调试器或类似的东西,逐行执行代码。无论如何,这都是一个很好的锻炼。
-
我是农业工程师,我不懂调试器
-
无意冒犯,但您的评论刚刚出现在我的个人 Stackoverflow 名言列表中。 “我是一名农业工程师。我不懂调试器。”漂亮。
-
有人认为这个问题与另一个名字相似的用户的another question 非常相似吗?
-
但是说真的,调试器是一种用于调试的工具,即帮助从代码中删除错误(如:问题/错误)。它的主要功能之一是它可以让你“逐步”执行程序,通常大致对应“逐行”,同时观察变量的值如何变化。
标签: c++ arrays string function