题目描述
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.
题目大意:
先输入一个整数t,代表下面有t组测试,对于每一组测试,先输入一个整数n,下面n行每行输入六个0到49的整数,计算每个整数出现的次数,找到出现次数最多的6个整数,按从小到大的顺序输出,且若7出现的次数与其他数字出现的次数相同,优先考虑7.
解题思路:
记录一下每一个数字出现的次数,将其存到一个结构体中,然后按照出现次数的大小对结构体进行排序,先将前5位存起来,若7已被存起,则继续存入第六位,若7没被存入,则比较7出现的次数与第六个元素出现的次数的大小,如果相等,则存入7,否则存入第六项。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
struct node
{
int x;
int num;
}arr[1200];
map<int,int> ma;
bool cmp(node a,node b) {
if(a.num==b.num) return a.x<b.x;
return a.num>b.num;
}
int arr1[10];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
//freopen("out.txt", "w", stdout);
ios::sync_with_stdio(0),cin.tie(0);
int t;
cin>>t;
while(t--) {
int n;
cin>>n;
int nape,num7=0;
ma.clear();
int cnt=0;
rep(i,1,n) {
rep(j,1,6) {
cin>>nape;
if(nape==7) num7++;
if(ma[nape]!=0) arr[ma[nape]].num++;
else {
cnt++;
ma[nape]=cnt;
arr[ma[nape]].x=nape;
arr[ma[nape]].num=1;
}
}
}
sort(arr+1,arr+1+cnt,cmp);
bool ju=false;
rep(i,1,5) {
arr1[i]=arr[i].x;
if(arr[i].x==7) ju=true;
}
if(ju==false) {
if(num7==arr[6].num) arr1[6]=7;
else arr1[6]=arr[6].x;
}
else arr1[6]=arr[6].x;
sort(arr1+1,arr1+7);
rep(i,1,6) cout<<arr1[i]<<" ";
cout<<endl;
}
return 0;
}