任务说明:将杂乱无章的数据变得有规律。有各种各样的排序算法,看情况使用。
这里有空还是把各种排序算法总结下吧。qsort需要会写。。
P1177 【模板】快速排序
这个题目懒得写了,直接sort了...
以后要补上..
sort版本可以忽略了orz
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <algorithm> 5 6 using namespace std; 7 8 int main() { 9 int N; 10 cin >> N; 11 vector<int> vec(N); 12 for (int i = 0; i < N; ++i) { 13 cin >> vec[i]; 14 } 15 sort(vec.begin(), vec.end()); 16 if (N > 0) { 17 printf("%d", vec[0]); 18 for (int i = 1; i < N; ++i) { 19 printf(" %d", vec[i]); 20 } 21 printf(" "); 22 } 23 return 0; 24 25 }
P1059 明明的随机数
要求排序+去重
我直接sort -> unique -> resize 了...
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <algorithm> 5 6 using namespace std; 7 8 int main() { 9 int N; 10 cin >> N; 11 vector<int> vec(N); 12 for (int i = 0; i < N; ++i) { 13 cin >> vec[i]; 14 } 15 sort(vec.begin(), vec.end()); 16 auto iter = unique(vec.begin(), vec.end()); 17 vec.resize(std::distance(vec.begin(), iter)); 18 printf("%d ", vec.size()); 19 if (N > 0) { 20 printf("%d", vec[0]); 21 for (int i = 1; i < vec.size(); ++i) { 22 printf(" %d", vec[i]); 23 } 24 printf(" "); 25 } 26 return 0; 27 28 }
P1068 分数线划定
感觉比模拟题还水,忽略吧
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <utility> 6 7 using namespace std; 8 9 typedef pair<int, int> P; 10 11 bool cmp(P p1, P p2) { 12 if (p1.second != p2.second) { 13 return p1.second > p2.second; 14 } else { 15 return p1.first < p2.first; 16 } 17 } 18 19 20 int main() { 21 int n, m; 22 cin >> n >> m; 23 const int bar = m * 3 / 2 ; 24 //printf("bar = %d ", bar); 25 26 vector<P> vec(n); 27 for (int i = 0; i < n; ++i) { 28 cin >> vec[i].first >> vec[i].second; 29 } 30 sort(vec.begin(), vec.end(), cmp); 31 if (bar == 0) { 32 printf("0 "); 33 return 0; 34 } 35 const int score = vec[bar-1].second; 36 //printf("score = %d ", score); 37 int idx = bar-1; 38 while(idx < n && vec[idx].second == score) { 39 ++idx; 40 } 41 //printf("final idx = %d ", idx); 42 printf("%d %d ", score, idx); 43 for(int i = 0; i < idx; ++i) { 44 printf("%d %d ", vec[i].first, vec[i].second ); 45 } 46 47 return 0; 48 }
P1781 宇宙总统
候选人有编号和选票,输出最大票数候选人的编号和选票。因为选票很大,所以不能用long long型。用string类型来比较。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <utility> 6 #include <string> 7 #include <cstring> 8 9 using namespace std; 10 11 typedef pair<int, int> P; 12 13 int main() { 14 int n; 15 cin >> n; 16 vector<string> vec(n); 17 string str; 18 for (int i = 0 ; i < n; ++i) { 19 cin >> str; 20 vec[i] = str; 21 } 22 string strMax = vec[0]; 23 int ansIdx = 0; 24 for (int i = 1; i < n; ++i) { 25 if (vec[i].size() > strMax.size()) { 26 strMax = vec[i]; 27 ansIdx = i; 28 } else if (vec[i].size() == strMax.size() && vec[i] > strMax) { 29 strMax = vec[i]; 30 ansIdx = i; 31 } 32 } 33 cout << ansIdx+1 << endl; 34 cout << strMax << endl; 35 return 0; 36 }