排序题目练习(Ignatius and the Pincess IV、排序、Clock、排名)

HDJ排序题目练习

HDU Sort

  1. HDU 1029 Ignatius and the Pincess IV
  2. HDU 1106 排序
  3. HDU 1209 Clock
  4. HDU 1236 排名      
 
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?
 


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’组成。
 

 

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,所以\0也是一个分界符
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]!='\0')
40         {
41             while (s[j]!='5'&&s[j]!='\0') j++;
42             k=j;
43             while (s[k]=='5') k++;
44             push_into_vector(i,j);
45             i=k;j=i;
46         }
47         sort(a.begin(),a.end());
48         for (i=0;i<a.size()-1;i++) cout << a[i] <<" ";
49         cout << a[i]<<endl;
50     }
51     return 0;
52 }

Clock

 

Problem Description

 

There is an analog clock with two hands: an hour hand and a minute hand. The two hands form an angle. The angle is measured as the smallest angle between the two hands. The angle between the two hands has a measure that is greater than or equal to 0 and less than or equal to 180 degrees.

Given a sequence of five distinct times written in the format hh : mm , where hh are two digits representing full hours (00 <= hh <= 23) and mm are two digits representing minutes (00 <= mm <= 59) , you are to write a program that finds the median, that is, the third element of the sorted sequence of times in a nondecreasing order of their associated angles. Ties are broken in such a way that an earlier time precedes a later time.

For example, suppose you are given a sequence (06:05, 07:10, 03:00, 21:00, 12:55) of times. Because the sorted sequence is (12:55, 03:00, 21:00, 06:05, 07:10), you are to report 21:00.

 

 


Input

 

The input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case is given on a single line, which contains a sequence of five distinct times, where times are given in the format hh : mm and are separated by a single space.

 

 


Output

 

Print exactly one line for each test case. The line is to contain the median in the format hh : mm of the times given. The following shows sample input and output for three test cases.

 

 


Sample Input

 

3 00:00 01:00 02:00 03:00 04:00 06:05 07:10 03:00 21:00 12:55 11:05 12:05 13:05 14:05 15:05

 

 


Sample Output

 

02:00 21:00 14:05

 

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <queue>
 4 #include <string.h>
 5 #include <algorithm>
 6 #include <map>
 7 #include <vector>
 8 #include <math.h>
 9 
10 using namespace std;
11 /*
12 abs:int 
13 fabs:float 
14 这里除以2极有可能是浮点类型的
15 */
16 struct node
17 {
18     int h,m;
19     float angle;
20 }a[6];
21 int cmp(node x,node y)
22 {
23     if(x.angle!=y.angle)
24     return x.angle<y.angle;
25     return x.h<y.h; // 角度相同时按照小时进行排序
26 }
27 int main()
28 {
29     int t,hh;
30     while (cin>>t)
31     {
32         while (t--)
33         {
34             for (int i=0;i<5;i++)
35             {
36                 scanf("%d:%d",&a[i].h,&a[i].m);
37                 // 作为整型的输入方式即可,用字符串太麻烦
38             //    cout << a[i].h<<":"<<a[i].m<<endl;
39 
40                 if (a[i].h>=12) hh=a[i].h-12;
41                 else hh=a[i].h;
42                     a[i].angle=fabs(30.0*(hh)-6.0*a[i].m+a[i].m/2.0);
43                 if (a[i].angle>180) a[i].angle=360-a[i].angle;
44             }
45             sort(a,a+5,cmp);
46             printf("%02d:%02d\n",a[2].h,a[2].m);
47 
48         }
49     }
50 
51 
52     return 0;
53 }

 

 排名

 

今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 
每题的分值,所以并不是最后的排名。给定录取分数线,请你写程序找出最后通过分数线的 
考生,并将他们的成绩按降序打印。 

Input

 

测试输入包含若干场考试的信息。每场考试信息的第1行给出考生人数N ( 0 < N 
< 1000 )、考题数M ( 0 < M < = 10 )、分数线(正整数)G;第2行排序给出第1题至第M题的正整数分值;以下N行,每行给出一 
名考生的准考证号(长度不超过20的字符串)、该生解决的题目总数m、以及这m道题的题号 
(题目号由1到M)。 
当读入的考生人数为0时,输入结束,该场考试不予处理。 

 

 


Output

 

对每场考试,首先在第1行输出不低于分数线的考生人数n,随后n行按分数从高 
到低输出上线考生的考号与分数,其间用1空格分隔。若有多名考生分数相同,则按他们考 
号的升序输出。 

 

 


Sample Input

 

4 5 25 10 10 12 13 15 CS004 3 5 1 3 CS003 5 2 4 1 3 5 CS002 2 1 2 CS001 3 2 3 5 1 2 40 10 30 CS001 1 2 2 3 20 10 10 10 CS000000000000000001 0 CS000000000000000002 2 1 2 0

 

 


Sample Output

 

3 CS003 60 CS001 37 CS004 37 0 1 CS000000000000000002 20

 

 

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <queue>
 4 #include <string.h>
 5 #include <algorithm>
 6 #include <map>
 7 #include <vector>
 8 #include <math.h>
 9 
10 /*
11 成绩相同时按照考号升序进行排序;
12 考察的主要是结构体数据的排序过程。
13 
14 */
15 using namespace std;
16 int n,m,line;
17 int grade[11];
18 struct stu
19 {
20     char sno[50];
21     int num;
22     int no[11];
23 }s[1001];
24 bool cmp(const stu &a,const stu &b)
25 {
26     if (a.num!=b.num)
27     return a.num > b.num;
28     else
29     {
30         return strcmp(a.sno,b.sno)<0;
31     }
32 
33 }
34 int main()
35 {
36     while (scanf("%d%d%d",&n,&m,&line)!=EOF)
37     {
38         if (n==0) break;
39         for (int i=0;i<m;i++) scanf("%d",&grade[i]);
40 
41         int sum;
42         int cnt=0;
43         for (int i=0;i<n;i++)
44         {
45             scanf("%s %d",&s[i].sno,&s[i].num);
46             sum=0;
47             for (int j=0;j<s[i].num;j++)
48             {
49                 scanf("%d",&s[i].no[j]);
50                 sum+=grade[s[i].no[j]-1];
51             }
52             s[i].num=sum;
53             if (sum>=line) cnt++;
54         }
55         sort(s,s+n,cmp);
56         int i=0;
57         printf("%d\n",cnt);
58         while (n--&&s[i].num>=line)
59         {
60             printf("%s %d\n",s[i++].sno,s[i].num);
61         }
62 
63 
64     }
65 
66 
67     return 0;
68 }

 

 

 

 

八大排序算法复习

 

posted @ 2018-08-21 20:50  暴走的二萌  阅读(499)  评论(0编辑  收藏  举报