題目描述
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_JUDGEfreopen("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;
}
?