JOJ 1101 DNA Sorting

JOJ 1101 DNA Sorting

改了浮点数转整数的错误以后又PE了2次,终于AC了~
题意很明了,就是求逆序数后在排序输出
方法
是将每组数中的measure参与排序,然后依次提取下标,输出对应的字符串就可以了

 1 #include < iostream >
 2 #include < string >
 3 #include < vector >
 4 #include < algorithm >
 5 using namespace std;
 6 int  m,n;
 7 int  deal( string  s)
 8 {
 9      int  i,j,sum = 0 ;
10      for (i = 0 ;i < m;i ++ )
11          for (j = i + 1 ;j < m;j ++ )
12         {
13              if (s[i] > s[j]) ++ sum;
14         }
15     return sum;
16 }
17
18 int  main()
19 {
20      int  ncase,i,j;
21     cin >> ncase;
22      double  yoyo;
23      string  s;
24     
25      while (ncase)
26     {
27         vector < double > v;
28         vector < string > str;
29         cin >> m;
30         cin >> n;
31          for (i = 0 ;i < n;i ++ )
32         {
33             cin >> s;
34             str.push_back(s);
35             v.push_back(deal(s) + ( double )i / 100 );   
36         }
37         sort(v.begin(),v.end());
38          for (i = 0 ;i < n;i ++ )
39         {
40             j = ( int )(v[i] * 100 + 0.5 )% 100 ;
41             cout << str[j] << endl;
42         }
43          -- ncase;
44          if (ncase)cout << endl;    
45     }
46
47
48 }


方法
还不会用map用关键字排序,就只好自己用苯方法又作了一遍,写得还是冗余阿

 1 #include < iostream >
 2 #include < string >
 3 #include < vector >
 4 #include < algorithm >
 5 using namespace std;
 6 int  m,n;
 7 struct Point
 8 {
 9      string  s;
10      int  num;
11 };
12 int  deal( string  s)
13 {
14      int  i,j,sum = 0 ;
15      for (i = 0 ;i < m;i ++ )
16          for (j = i + 1 ;j < m;j ++ )
17         {
18              if (s[i] > s[j]) ++ sum;
19         }
20     return sum;
21 }
22 int  main()
23 {
24      int  ncase,i,j;
25
26     cin >> ncase;
27      string  str;
28     Point p;
29     
30      while (ncase)
31     {
32     vector < Point > v;
33     vector < int > s;
34         cin >> m;
35         cin >> n;
36
37          for (i = 0 ;i < n;i ++ )
38         {
39             cin >> str;
40             p.s = str;
41             p.num = deal(str);
42             v.push_back(p);
43             s.push_back(p.num);
44         }  
45         sort(s.begin(),s.end());
46         vector < Point > ::iterator it;
47         
48          for (i = 0 ;i < n;i ++ )
49              for (it = v.begin();it! = v.end();it ++ )
50             {
51                  if (( * it).num == s[i])
52                 {
53                     cout << ( * it).s << endl;
54                     v.erase(it);
55                     break;
56                 }
57             }
58          -- ncase;
59          if (ncase)
60         cout << endl;
61         
62     }
63 }



 

你可能感兴趣的:(JOJ 1101 DNA Sorting)