问题描述
- 该段程序是字符串冒泡排序,请大神找bug,做了好久了
-
#include
#include
using namespace std;
int c;cin >> c; char b[100][100]; for (int m = 0; m<c; m++){ cin >> b[m]; } int n; for (int j = 0; j < c; j++){ for (int m = 0; m<c ; m++){ if (strlen(b[j])>strlen(b[m])) n = strlen(b[j]); else n = strlen(b[m]); for (int d = 0; d <n; d++){ if ((b[m][d] == ''&&b[j][d] != '') || (int(b[m][d])<int(b[j][d]))) { char w[20]; for (unsigned int a = 0; a < strlen(b[j]); a++) { w[a] = b[j][a]; } w[strlen(b[j]) ]= ''; for (unsigned int a = 0; a< strlen(b[m]); a++) { b[j][a] = b[m][a]; } b[j][strlen(b[m])] = ''; for (unsigned int a = 0; a < strlen(w); a++) { b[m][a] = w[a]; } b[m][strlen(w)] = ''; break; } } } } for (int m = 0; m<c; m++){ cout << b[m] << " "; } return 0;
}
解决方案
你的输入只输入了二维数组的第一行吧。用二维数组干什么。
解决方案二:
#include
using namespace std;
int main()
{
int c;
cin >> c;
char b[100][100];
for (int m = 0; m
cin >> b[m];
}
int n;
for (int j = 0; j < c-1; j++){
for (int m = j+1; m
if (strlen(b[j])>strlen(b[m]))
n = strlen(b[j]);
else
n = strlen(b[m]);
for (int d = 0; d <n; d++){
if (b[m][d]<b[j][d])
{
char tmp[100];
strcpy(tmp,b[m]);
strcpy(b[m],b[j]);
strcpy(b[j],tmp);
break;
}
}
}
}
for (int m = 0; m<c; m++){
cout << b[m] << " ";
}
getchar();
getchar();
return 0;
}
解决方案三:
if (strlen(b[j])<strlen(b[m]))
n = strlen(b[j]);
else
n = strlen(b[m]);