UVa 482 - Permutation Arrays

本文详细介绍了程序代码优化的策略,包括如何通过合理的数据处理方式提高代码效率。通过实例解析,展示了如何将字符串转化为整数数组,并进行了排序与输出。文章特别强调了对于数据大小不确定情况下的灵活处理策略,提供了使用`string`和`vector`等动态类型替代静态数组的方法,以适应不同规模的数据需求。

  题目大意:引用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

 

转载于:https://www.cnblogs.com/xiaobaibuhei/p/3289251.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值