题目描述
Your friend Superstitious Stanley is always getting himself into trouble. This time, in his Super Lotto Pick and Choose plan, he wants to get rich quick by choosing the right numbers to win the lottery. In this lottery, entries consist of six distinct integers from 1 to 49, which are written in increasing order. Stanley has compiled a list of winning entries from the last n days, and is going to use it to pick his winning numbers.
In particular, Stanley will choose the six numbers that appeared the most often. When Stanley is breaking ties, he prefers smaller numbers, except that he prefers seven to every other number. What is Stanley’s entry?
输入
The first line of input contains a single integer T (1 ≤ T ≤ 100), the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 1,000), the number of winning entries that Stanley compiled. The next n lines each contain a lottery entry as described above.
输出
For each test case, output a single line containing Stanley’s entry.
样例输入
复制样例数据
2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9 10 11 12 3 1 2 3 4 5 6 4 5 6 7 8 9 1 2 3 7 8 9
样例输出
4 5 6 7 8 9 1 2 3 4 5 7
提示
In the first test case, the numbers 4 through 9 appear twice each, while all other numbers appear at most
one time.
In the second test case, all numbers 1 through 9 appear twice each. The tiebreaking rule means Stanley
prioritizes picking 7 and then the five smallest numbers。
题意:给出n组数,每组都是6个,按从小到大的顺序输出出现次数最多的数,在同等条件下(出现次数相同),有7则要7。
题解:用结构体把所有数存下来,记录每个数次数(数的范围是1-49),先排序,然后输出前五个(输出结果也需要从小到大排序!!)再考虑7(只有7和应该输出的第六位出现的次数相等时才优先输出7),遍历一遍看后续数中是否含有7(1.如果前面已经输出7则后面找不到 2、有7但是7出现次数不满足条件不需要输出),通过上一步可把所有数都找到从小到大排序输出即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int result[7];
int flag;
struct node{
node(){
x = 0;
time = 0;
}
int x;
int time;
}nodes[50];
struct rule{
bool operator()(const node&a,const node&b){
if(a.time == b.time)
return a.x < b.x;
return a.time > b.time;
}
};
int main()
{
int t;
int p;
int l;
int flag;
cin>>t;
int n;
while(t--){
memset(result,0,sizeof(result));
memset(nodes,0,sizeof(nodes));
p = 0;
flag = 0;
cin>>n;
for(int i = 0;i < 6*n;i++){
int X;
cin>>X;
nodes[X].x = X;
nodes[X].time++;
}
sort(nodes,nodes+50,rule());
for(int i = 0;i < 50;i++){
if(p == 5)
break;
if(nodes[i].x != 0){
result[p++] = nodes[i].x;
}
}
int Max = nodes[4].time;
for(int i = 5;i < 50;i++){
if(nodes[i].time == Max && nodes[i].x == 7){
result[p++] = 7;
flag = 1;
break;
}
}
if(flag == 0){
result[p++] = nodes[5].x;
}
sort(result,result+6);
for(int i = 0;i < 6;i++){
if(i==0)
cout<<result[i];
else
cout<<" "<<result[i];
}
cout<<endl;
}
}