UVA 10905 Children's Game

 1 2
 2 3
 3 4
 4 5
 5 6
 6 7
 7 8
 8 9
 9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 #include <iostream>
26 #include <algorithm>
27 #include <string>
28 #include <stdio.h>
29 #include <string.h>
30 using namespace std;
31 bool cmp(string  a,string b)
32 {
33     return a+b>b+a;
34 }
35 string num[55];
36 int main()
37 {
38     int n,i;
39     while(cin>>n&&n)
40     {
41         for(i=0;i<n;i++)
42             cin>>num[i];
43         sort(num,num+n,cmp);
44         for(i=0;i<n;i++)
45             cout<<num[i];
46             cout<<endl;
47     }
48     return 0;
49 }
View Code

西姐代码,很不错,string类不需要考虑其他问题

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define FOR(i,b,e) for(int i = b; i <= e; i++)
 6 #define sc(x) scanf("%d", &x)
 7 #define sc1(x) scanf("%s", &x)
 8 using namespace std;
 9 const int INF = 1000, MAX = 50;
10 struct Str{
11     char s[INF];
12 }num[MAX];
13 char str1[INF], str2[INF];
14 int cmp(const Str a, const Str b){//排序,将两两连接有AB或是BA,将如果AB>BA,那么就将A排在B的后面,反之则反之。
15     strcpy(str1, a.s);
16     strcat(str1, b.s);
17     strcpy(str2, b.s);
18     strcat(str2, a.s);
19     if(strcmp(str1, str2) > 0)
20         return 1;
21     return 0;
22 }
23 int main(int argc, char *argv[]) {
24     int N, i;
25     while(sc(N), N){
26         FOR(i, 0, N-1)    sc1(num[i].s);
27         sort(num, num+N, cmp);
28         FOR(i, 0, N-1)
29         printf("%s",num[i].s);
30         printf("\n");
31     }
32     return 0;
33 }
View Code

这个代码考虑到结构体,因此那个com函数是必须的

int cmp(const Str &a, const Str &b){
strcpy(str1, a.s);
strcat(str1, b.s);
strcpy(str2, b.s);
strcat(str2, a.s);
if(strcmp(str1, str2) > 0)
return 1;
return 0;
}

posted @ 2015-04-01 18:36  PastLIFE  阅读(83)  评论(0编辑  收藏  举报