本帖最后由 jackz007 于 2024-3-9 16:56 编辑
【代码1】:#include<iostream>
#include<cstring>
using namespace std ;
int main(void)
{
char str[10][64] , temp[64] ;
for(int i = 0 ; i < 10 ; i ++) gets(str[i]) ;
for(int i = 0 ; i < 10 - 1 ; i ++) {
for(int j = i + 1 ; j < 10 ; j ++) {
if(strcmp(str[i] , str[j]) > 0) {
strcpy(temp , str[i]) ;
strcpy(str[i] , str[j]) ;
strcpy(str[j] , temp) ;
}
}
}
for(int i = 0 ; i < 10 ; i ++) cout << str[i] << endl ;
}
【代码2】:#include<iostream>
#include<cstring>
using namespace std ;
int main(void)
{
string str[10] ;
for(int i = 0 ; i < 10 ; i ++) cin >> str[i] ;
for(int i = 0 ; i < 10 - 1 ; i ++) {
for(int j = i + 1 ; j < 10 ; j ++) {
if(str[i] . compare(str[j]) > 0) {
string temp = str[i] ;
str[i] = str[j] ;
str[j] = temp ;
}
}
}
for(int i = 0 ; i < 10 ; i ++) cout << str[i] << endl ;
}
|