HDJ排序题目练习
HDU Sort
Problem Description
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1
Sample Output
3 5 1
思路:用map存储输入数据并计算每个数出现的次数后,map容器按照key自动排序,但value的排序需要通过自定义的结构体pai进行转储,然后对结构体进行排序便得到value最大的数字。
注意map进行循环遍历的方法以及调用key和value的方法。
1 #include <iostream> 2 #include <stdio.h> 3 #include <queue> 4 #include <string.h> 5 #include <algorithm> 6 #include <map> 7 8 using namespace std; 9 10 /* 11 找到最少出现(n+1)/2次的special number 12 */ 13 const int maxn=1000000; 14 map<int,int> m; 15 struct pai 16 { 17 int key,value; 18 }p[maxn]; 19 bool cmp(const pai &a,const pai &b) 20 { 21 return a.value<b.value; 22 } 23 int main() 24 { 25 int n,x,i; 26 map<int,int>::iterator iter; 27 while (cin >>n) 28 { 29 m.clear(); // map容器的清空方法? 30 // m.erase(m.begin(),m.end()); 31 for (int i=0;i<n;i++) 32 { 33 cin >>x; 34 m[x]++; 35 } 36 // 把map中的键值对转储到pair中形成结构体数组 37 int k=0; 38 iter=m.begin(); 39 while (iter!=m.end()) 40 { 41 p[k].key=iter->first; 42 p[k].value=iter->second; 43 k++;iter++; 44 } 45 46 sort(p,p+k,cmp); 47 cout <<p[k-1].key<<endl; 48 } 49 return 0; 50 }
map的基本使用方法:https://www.cnblogs.com/empty16/p/6395813.html
map的迭代遍历方法:https://blog.csdn.net/u010429424/article/details/75332700
排序
Problem Description
输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。
你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。
你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。
Input
输入包含多组测试用例,每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。
输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。
输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。
Output
对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。
Sample Input
0051231232050775
Sample Output
0 77 12312320
1 #include <iostream> 2 #include <stdio.h> 3 #include <queue> 4 #include <string.h> 5 #include <algorithm> 6 #include <map> 7 #include <vector> 8 9 using namespace std; 10 /* 11 可能会有连续的5出现,不应该只跳过一个五 12 13 最后一个数字可能不是5,所以 也是一个分界符 14 */ 15 vector<int> a; 16 char s[1001]; 17 void push_into_vector(int i,int j) 18 { 19 // s[i-j-1]进入vector中 20 if (i==j) return ; 21 int sum=0; 22 int k=1; 23 int t=j-i; 24 while (t--) 25 { 26 sum+=(s[--j]-'0')*k; 27 k=k*10; 28 } 29 a.push_back(sum); 30 } 31 int main() 32 { 33 34 int i,j,k; 35 while (cin>>s) 36 { 37 i=0;j=0; 38 while (!a.empty()) a.pop_back(); 39 while (s[i]!='