问题 I: Ingenious Lottery Tickets
时间限制: 1 Sec 内存限制: 128 MB
提交: 758 解决: 190
[提交] [状态] [命题人:admin]
题目描述
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.
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct Num{
int num;
int cnt;
}s[105];
bool cmp(Num a,Num b){
if(a.cnt==b.cnt)
return a.num<b.num;
else
return a.cnt>b.cnt;
}
int main(){
int t,n,x;
cin>>t;
while(t--){
cin>>n;
memset(s,0,sizeof(s));
int flag;
int j=0;
for(int i=0;i<6*n;i++){
cin>>x;
flag=0;
for(int k=0;k<j;k++){
if(s[k].num==x){
s[k].cnt++;
// cout<<"*";
flag=1;
}
}
if(flag==0){
s[j].num=x;
s[j].cnt++;
j++;
// cout<<j<<" ";
}
//cout<<s[i].cnt<<" "<<s[i].num<<endl;
}
sort(s,s+j,cmp);
int r[6];
memset(r,0,sizeof(r));
for(int i=0;i<5;i++){
r[i]=s[i].num;
}
int tmp1=s[5].cnt;
int flag1=0;
for(int i=5;i < j;i++){
if(s[i].num==7&&s[i].cnt==tmp1){
r[5]=7;
flag1=1;
break;
}
}
if(flag1==0){
r[5]=s[5].num;
}
sort(r,r+6);
for(int i = 0;i < 5;i ++){
cout<<r[i]<<" ";
}
cout<< r[5] <<endl;
}
return 0;
}
解析稍后再更