对于字符串排序的问题,VC中提供了一个qsort函数,但至今还不会使用,只得使用自己的笨办法来解决了,感觉时间效率还是很低啊,虽说已经AC了,但还是不尽人意。。。。望高手就此题指点一下在下。。。。。此题为Uva online 上的一道题,试题链接为:
uva.onlinejudge.org/index.php
#
include < iostream >
#include < cstring >
#include < cmath >
using namespace std;
const int MAX = 2100;
const int MIN = 100;
typedef struct CN {
int num;
char CountryName[MIN];
}
CounTry;
int main() {
int n, m = 0, i, j, len, a;
CounTry Country[MAX];
char Name[MIN];
char NAME[MIN];
cin >> n;
getchar();
//吃掉回车符
for
(i = 0; i < n; i++)
//将城市的数目初置为0
Country[i].num = 0;
for
(i = 0; i < n; i++) {
int tag = 1;
int flag = 0;
int k = 0, l;
cin.getline(Name, MIN);
len = strlen(Name);
for
(j = 0; j < len; j++) {
if
(Name[j] ==
' '
) {
tag = 2;
NAME[k] =
'/0'
;
for
(l = 0; l < m; l++) {
if
(strcmp(Country[l].CountryName, NAME) == 0) {
flag = 1;
Country[l].num++;
m--;
break
;
}
}
if
(!flag) {
Country[m].num++;
strcpy(Country[m].CountryName, NAME);
}
break
;
}
else
if
(Name[j] !=
' '
&& tag == 1)
NAME[k++] = Name[j];
}
//for
m++;
//记录下一个城市
}
//for
for
(a = 1; a <= m; a++) {
for
(i = 0; i < m; i++) {
char * p;
int location;
p = Country[i].CountryName;
if
(Country[i].num > 0) {
for
(j = 0; j < m; j++) {
if
(Country[j].num > 0 && strcmp(Country[j].CountryName, p) <= 0) {
location = j;
p = Country[j].CountryName;
}
}
}
if
(Country[location].num > 0)
cout << p <<
" "
<< Country[location].num << endl;
Country[location].num = -1;
//改变城市数量以便判别是否已经输出
}
}
return
0;
}
|