UVa 482 - Permutation Arrays

  题目大意:引用Worlf of Seven的描述,

What the problem wants is:

3 1 2
32.0 54.7 -2

54.7 is the 1st position in array
-2 is the 2nd position in array
32.0 is on the 3rd position in array

  直接进行转化就好了,还有就是题目要求浮点数的输出和输入形式一样,所以浮点数用字符串存储就好了。

 1 #include <cstdio>
 2 #include <cctype>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define MAXN 1000000
 7 
 8 struct Num 
 9 {
10     char s[20];
11     int rank;
12     bool operator < (const Num& n) const
13     {
14         return rank < n.rank;
15     }
16 };
17 Num num[MAXN];
18 int idx[MAXN];
19 
20 int main()
21 {
22 #ifdef LOCAL
23     freopen("in", "r", stdin);
24 #endif
25     int T;
26     scanf("%d", &T);
27     getchar();
28     char str[MAXN];
29     while (T--)
30     {
31         gets(str);
32         gets(str);
33         int len = strlen(str);
34         int n = 0;
35         for (int i = 0; i < len; )
36         {
37             if (isdigit(str[i]))
38             {
39                 int t = str[i] - '0';
40                 i++;
41                 while (isdigit(str[i]))
42                 {
43                     t = t*10 + str[i] - '0';
44                     i++;
45                 }
46                 n++;
47                 num[n].rank = t;
48             }
49             else i++;
50         }
51         for (int i = 1; i <= n; i++)
52             scanf("%s", num[i].s);
53         sort(num+1, num+n+1);
54         for (int i = 1; i <= n; i++)
55             printf("%s\n", num[i].s);
56         getchar();
57         if (T)  printf("\n");
58     }
59     return 0;
60 }
View Code 1

  刚开始WA了,看代码感觉没错,看论坛说因为题目没有规定数据大小,所以...于是把MAXN从104改成106,然后就AC了...写程序的时候还感觉104已经够大了呢,看来以后对这种没规定数据量大小的题还是用string,vector这种动态大小的类型好了。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cctype>
 4 #include <cstring>
 5 #include <vector>
 6 #include <string>
 7 #include <algorithm>
 8 using namespace std;
 9 
10 struct Num 
11 {
12     string s;
13     int rank;
14     bool operator < (const Num& n) const
15     {
16         return rank < n.rank;
17     }
18 };
19 vector<Num> num;
20 
21 int main()
22 {
23 #ifdef LOCAL
24     freopen("in", "r", stdin);
25 #endif
26     int T;
27     scanf("%d", &T);
28     getchar();
29     string str;
30     while (T--)
31     {
32         getline(cin, str);
33         getline(cin, str);
34         int len = str.size();
35         num.clear();
36         for (int i = 0; i < len; )
37         {
38             if (isdigit(str[i]))
39             {
40                 int t = str[i] - '0';
41                 i++;
42                 while (isdigit(str[i]))
43                 {
44                     t = t*10 + str[i] - '0';
45                     i++;
46                 }
47                 num.push_back((Num){"", t});
48             }
49             else i++;
50         }
51         for (int i = 0; i < num.size(); i++)
52             cin >> num[i].s;    
53         sort(num.begin(), num.end());
54         for (int i = 0; i < num.size(); i++)
55             cout << num[i].s << endl;
56         getchar();
57         if (T)  printf("\n");
58     }
59     return 0;
60 }
View Code 2

 

posted @ 2013-08-29 15:29  xiaobaibuhei  阅读(240)  评论(0)    收藏  举报